Tkinter 网格管理器中的 Python 类
我对 Tkinter 的 GRID 几何管理器或自定义类或两者都有问题。最有可能两者皆有。代码如下:
import sys
from tkinter import
def main():
app = App()
app.master.title("Sample application")
app.mainloop()
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid(sticky=N+S+E+W)
top = self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self.__createWidgets()
def __createWidgets(self):
self.f1 = Frame(self, height=100, width=200,
bg='green')
self.f1.grid(row=0, column=0, sticky=E+W)
self.f2 = Frame( self, bg= "yellow", height=100, width=200 ) ############################# CHANGE to self.f2 = myTestFrame( self )
self.f2.grid(row=1, sticky = N+S+E+W)
self.f3 = Frame( self, bg = "cyan", height = 100, width = 200 )
self.f3.grid(row=2, sticky = E+W)
self.quitButton = Button ( self, text="Quit", command=self.quit )
self.quitButton.grid(row=4, column=0, columnspan=100,
sticky=E+W)
class myTestFrame( Frame ):
def __init__( self, parent):
Frame.__init__(self, None)
self.myText = Text( self )
self.myText.grid()
#self.testFrame = Frame ( self, bg = "white", height = 100, width = 300 )
#self.testFrame.grid()
if __name__ == "__main__":
main()
`
现在问题是:这段代码完成了我想要它做的所有事情。这里有三个框架,顶部框架 self.f1
、中间框架 self.f2
、底部框架 self.f3
和一个简单的退出按钮。顶部(第 0 行)和底部(第 2 行)框架水平调整大小(第 0 列),中间框架的行(第 1 行,第 0 列)接收水平和垂直调整大小的所有权重。一切都很好......直到我尝试实现我自己的类myTestFrame
。
每当我尝试使用 Text()
小部件填充 self.f2
中的自定义框架时,事情就会变得混乱。它显示默认的文本小部件,但我的大小调整变得很奇怪,网格放置很疯狂。到底是怎么回事?
编辑!!! unutbu 下面的回答肯定有助于解决班级问题。现在,网格管理器正在将文本小部件正确放置在中框 self.f2
内,但是,即使我在 myText.grid(sticky = N+S+E+W )
它无法正确扩展。所以我想到了以下内容:
1)通过使用class App
的def __init__
构造函数将根窗口设置为可调整大小,我将其设置为第一行和零列应用程序窗口的大小将在 self.rowconfigure( 1, Weight = 1)
和 self.columnconfigure( 0, Weight = 1)
中接收用于调整大小的所有空间分配。
2) 现在框架 self.f2
已放置在第 1 行,它将接收 self.f2.grid(N+S+E+W)
的所有大小调整>。
3) 现在,我使用 text()
小部件填充此自定义框架,该小部件设置为通过声明 self.myText.grid( Sticky = N+S+ 来占据此框架中的所有空间E+W)
。
但是 text()
小部件没有填充 self.f2
!当我在没有自定义类的情况下正常执行此操作时,使用 sticky = N+S+E+W
属性可以正常工作。我的想法正确吗?
I am having a problem with either Tkinter's GRID geometry manager or custom classes or both. Most likely both. Here's the code:
import sys
from tkinter import
def main():
app = App()
app.master.title("Sample application")
app.mainloop()
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid(sticky=N+S+E+W)
top = self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self.__createWidgets()
def __createWidgets(self):
self.f1 = Frame(self, height=100, width=200,
bg='green')
self.f1.grid(row=0, column=0, sticky=E+W)
self.f2 = Frame( self, bg= "yellow", height=100, width=200 ) ############################# CHANGE to self.f2 = myTestFrame( self )
self.f2.grid(row=1, sticky = N+S+E+W)
self.f3 = Frame( self, bg = "cyan", height = 100, width = 200 )
self.f3.grid(row=2, sticky = E+W)
self.quitButton = Button ( self, text="Quit", command=self.quit )
self.quitButton.grid(row=4, column=0, columnspan=100,
sticky=E+W)
class myTestFrame( Frame ):
def __init__( self, parent):
Frame.__init__(self, None)
self.myText = Text( self )
self.myText.grid()
#self.testFrame = Frame ( self, bg = "white", height = 100, width = 300 )
#self.testFrame.grid()
if __name__ == "__main__":
main()
`
Now here's the problem: This piece of code does everything I want it to do. There are three frames here, the top frame self.f1
, the middle frame self.f2
, the bottom frame self.f3
, and a simple quit button. Top (row 0) and bottom (row 2) frames resize horizontally (column 0) and the middle frame's row (row 1, column 0) receives all of the weight for resizing horizontally and vertically. Everything is good... UNTIL I try to implement my own class myTestFrame
.
Whenever I try to populate a custom frame in self.f2
with say a Text()
widget, things go haywire. It displays the default text widget but my resizing goes funky, GRID placement is crazy. What is going on?
EDIT!!!
unutbu's answer below certainly helps with the class issue. Now the GRID manager is placing a text widget correctly inside the middle frame self.f2
, however, even when I set the sticky in myText.grid(sticky = N+S+E+W)
it does not expand correctly. So I thought the following:
1) By setting the root window to be resizable with the def __init__
constructor of class App
, I set it so that the first row and zeroth column of the App window would receive all of the space allotment for resizing in self.rowconfigure( 1, weight = 1)
and self.columnconfigure( 0, weight = 1)
.
2) Now that the frame self.f2
is placed in row 1, it will receive all of the resizing with self.f2.grid(N+S+E+W)
.
3) Now I populate this custom frame with a text()
widget that is set to occupy ALL of the space in this frame by stating self.myText.grid( sticky = N+S+E+W)
.
But the text()
widget is not filling self.f2
! When I do this normally without a custom class, using the sticky = N+S+E+W
attribute works correctly. Am I thinking about this correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Frame.__init__
需要传递给父级:Runnable 示例:
The
Frame.__init__
needs to be passed the parent:Runnable example: