2017-02-26 80 views
0

我的问题是我无法使用tkraise()来处理任何框架对象。当我使用变量来存储框架时,它确实有效,但在使用对象时没有。如何让tkraise()在我的框架对象上工作并显示blue_frame? (所有其他框架功能也不起作用)框架功能不适用于我创建的框架对象

对于您的信息:我有一个基本框架(框),其中所有其他框架进去。这些框架是我使用New_Frame类创建的对象,它继承了一切从Frame类 - 这意味着我应该能够对我的New_Frame类对象执行各种帧操作,但它们不工作,例如tkraise()。

from tkinter import * 

root = Tk() 

root.minsize(width=300, height=230) 

box = Frame(root) 
box.pack(fill=BOTH, expand=True) 
box.grid_columnconfigure(0, weight=1) 
box.grid_rowconfigure(0, weight=1) 

class New_frame(Frame): 

    def __init__(self,color): 
     Frame.__init__(self) 
     self.color = color 

     fr = Frame(box, bg=self.color) 
     fr.grid(row=0, column=0, sticky="nsew") 

# frame objects 
blue_frame = New_frame("blue") 
red_frame = New_frame("red") 
green_frame = New_frame("green") 

blue_frame.tkraise() 

root.mainloop() 
+0

你是打算建立一共有7帧(每'New_frame'内部具有帧)的?当你打电话给'tkraise'时,你打算提升哪一帧? –

回答

0

您创建框架的方式看起来非常不寻常。您正在创建两个框架,内部框架没有将外部框架作为父框架。

我的猜测是你打算让New_frame成为一个单一的框架。如果是这种情况,您需要从的New_frame中删除对Frame的呼叫。此外,在创建New_frame的实例时,您应该明确传入父级。

例如:

class New_frame(Frame): 

    def __init__(self,parent, color): 
     Frame.__init__(self, parent, bg=color) 
     self.color = color 
     self.grid(row=0, column=0, sticky="nsew") 

# frame objects 
blue_frame = New_frame(box, "blue") 
red_frame = New_frame(box, "red") 
green_frame = New_frame(box, "green")