创建后无法更改框架颜色
Unable to change frame color once created
下面的代码中,app是mainWindow的实例,继承自Tkinter.Frame。我正在尝试使用 Frame.Configure 方法更改 Frame.However 的背景颜色,调用 self.configure(background="yellow") 不起作用。有人可以帮助我了解我犯了什么错误吗?
import Tkinter
class mainWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, master=parent)
self.parent=parent
self.button1=Tkinter.Button(master=self.parent, text='ONE', command=self.change)
self.button1.pack()
self.pack()
def change(self):
self.parent.wm_title("Changed")
self.configure(background="yellow")
root = Tkinter.Tk()
root.geometry("600x600+50+50")
app=mainWindow(root)
root.mainloop()
尝试self.parent.configure(background="yellow")
我是 Tkinter
的新手(几分钟的新手),所以我根据您的代码猜测框架根本没有显示。框架的父级是 root
,也是按钮的父级。
所以在这里,我要更改根的(顶级小部件)背景
已编辑:
根据我上面的推理和 Marcin 的回答,我推断框架只是没有尺寸。所以这里是您的代码的编辑版本,框架已展开,框架将包含按钮。
import Tkinter
class mainWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, master=parent)
self.parent=parent
self.button1=Tkinter.Button(master=self, text='ONE', command=self.change)
self.button1.pack()
self.pack(fill=Tkinter.BOTH, expand=True)
def change(self):
self.parent.wm_title("Changed")
self.configure(background="yellow")
root = Tkinter.Tk()
root.geometry("600x600+50+50")
app=mainWindow(root)
root.mainloop()
它不起作用,因为你的Frame是"tiny"。它不包含任何小部件(按钮的父级是顶部 window,而不是框架)。所以要使框架变大,从而可见,您需要扩展它:
import Tkinter
class mainWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, master=parent)
self.parent=parent
self.button1=Tkinter.Button(master=self.parent,
text='ONE',
command=self.change)
self.button1.pack()
self.pack(fill=Tkinter.BOTH, expand=1) #<--- expand frame
def change(self):
self.parent.wm_title("Changed")
self.configure(background="yellow")
root = Tkinter.Tk()
root.geometry("600x600+50+50")
app=mainWindow(root)
root.mainloop()
下面的代码中,app是mainWindow的实例,继承自Tkinter.Frame。我正在尝试使用 Frame.Configure 方法更改 Frame.However 的背景颜色,调用 self.configure(background="yellow") 不起作用。有人可以帮助我了解我犯了什么错误吗?
import Tkinter
class mainWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, master=parent)
self.parent=parent
self.button1=Tkinter.Button(master=self.parent, text='ONE', command=self.change)
self.button1.pack()
self.pack()
def change(self):
self.parent.wm_title("Changed")
self.configure(background="yellow")
root = Tkinter.Tk()
root.geometry("600x600+50+50")
app=mainWindow(root)
root.mainloop()
尝试self.parent.configure(background="yellow")
我是 Tkinter
的新手(几分钟的新手),所以我根据您的代码猜测框架根本没有显示。框架的父级是 root
,也是按钮的父级。
所以在这里,我要更改根的(顶级小部件)背景
已编辑:
根据我上面的推理和 Marcin 的回答,我推断框架只是没有尺寸。所以这里是您的代码的编辑版本,框架已展开,框架将包含按钮。
import Tkinter
class mainWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, master=parent)
self.parent=parent
self.button1=Tkinter.Button(master=self, text='ONE', command=self.change)
self.button1.pack()
self.pack(fill=Tkinter.BOTH, expand=True)
def change(self):
self.parent.wm_title("Changed")
self.configure(background="yellow")
root = Tkinter.Tk()
root.geometry("600x600+50+50")
app=mainWindow(root)
root.mainloop()
它不起作用,因为你的Frame是"tiny"。它不包含任何小部件(按钮的父级是顶部 window,而不是框架)。所以要使框架变大,从而可见,您需要扩展它:
import Tkinter
class mainWindow(Tkinter.Frame):
def __init__(self, parent):
Tkinter.Frame.__init__(self, master=parent)
self.parent=parent
self.button1=Tkinter.Button(master=self.parent,
text='ONE',
command=self.change)
self.button1.pack()
self.pack(fill=Tkinter.BOTH, expand=1) #<--- expand frame
def change(self):
self.parent.wm_title("Changed")
self.configure(background="yellow")
root = Tkinter.Tk()
root.geometry("600x600+50+50")
app=mainWindow(root)
root.mainloop()