Tkinter 网格管理器中的 Python 类

发布于 2024-12-11 05:21:36 字数 2618 浏览 0 评论 0原文

我对 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 Appdef __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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-12-18 05:21:36

Frame.__init__ 需要传递给父级:

class myTestFrame( tk.Frame ):
    tk.Frame.__init__(self, parent, *args, **kwargs)

Runnable 示例:

import sys
import Tkinter as tk
E=tk.E
W=tk.W
N=tk.N
S=tk.S
def main():
    app = App()
    app.master.title("Sample application")
    app.mainloop()

class App(tk.Frame):
    def __init__(self, master=None):
        tk.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 = tk.Frame(self, height=100, width=200, bg='green')
        self.f1.grid(row=0, sticky=E+W)

        self.f2 = myTestFrame(self, bg='yellow', height=100, width=200)

        self.f3 = tk.Frame( self, bg = "cyan", height = 100, width = 200)
        self.f3.grid(row=2, sticky=E+W)

        self.quitButton = tk.Button(self, text="Quit", command=self.quit)
        self.quitButton.grid(row=4, column=0, sticky=E+W)

class myTestFrame( tk.Frame ):
    def __init__(self, parent, cnf={}, **kw):
        tk.Frame.__init__(self, parent, cnf, **kw)
        self.grid(row=1, sticky=N+S+E+W)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)                
        self.myText = tk.Text(self)
        self.myText.grid(row=0, sticky=N+S+E+W)

if __name__ == "__main__":
    main()

The Frame.__init__ needs to be passed the parent:

class myTestFrame( tk.Frame ):
    tk.Frame.__init__(self, parent, *args, **kwargs)

Runnable example:

import sys
import Tkinter as tk
E=tk.E
W=tk.W
N=tk.N
S=tk.S
def main():
    app = App()
    app.master.title("Sample application")
    app.mainloop()

class App(tk.Frame):
    def __init__(self, master=None):
        tk.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 = tk.Frame(self, height=100, width=200, bg='green')
        self.f1.grid(row=0, sticky=E+W)

        self.f2 = myTestFrame(self, bg='yellow', height=100, width=200)

        self.f3 = tk.Frame( self, bg = "cyan", height = 100, width = 200)
        self.f3.grid(row=2, sticky=E+W)

        self.quitButton = tk.Button(self, text="Quit", command=self.quit)
        self.quitButton.grid(row=4, column=0, sticky=E+W)

class myTestFrame( tk.Frame ):
    def __init__(self, parent, cnf={}, **kw):
        tk.Frame.__init__(self, parent, cnf, **kw)
        self.grid(row=1, sticky=N+S+E+W)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)                
        self.myText = tk.Text(self)
        self.myText.grid(row=0, sticky=N+S+E+W)

if __name__ == "__main__":
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文