4

我正在使用 Tkinter 的“grid()”方法为我的 python 程序设计 GUI。

有没有办法让小部件随主窗口缩放?

这是一个简短的示例:

from Tkinter import *

master = Tk()

Label(master, text="This is a test").grid(row=0, column=0)

mytext1 = Text(master, width=30, height=5)
mytext1.grid(row=1, column=0)

mytext2 = Text(master, width=30, height=5)
mytext2.grid(row=3, column=0)

master.mainloop()

我想做的是让小部件在主窗口的大小发生变化时调整它们的大小。(对于这么小的图形用户界面,这没有问题,但是当有很多小部件时,这变得可取。)

任何帮助将不胜感激!

4

2 回答 2

3

您可能希望将行/列配置为具有weight,以便在调整窗口大小时它们可以展开。此外,您希望小部件sticky位于其单元格的两侧。不仅frame行/列需要weight,而且 中的所有行和列也需要frame,因此是for循环。

from Tkinter import *

class MyFrame(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid(row=0, column=0, sticky=N+S+E+W)
        #Give the grid, column of the frame weight...
        Grid.rowconfigure(master, 0, weight=1)
        Grid.columnconfigure(master, 0, weight=1)
        self.create_widgets()

    def create_widgets(self):
        #Give the grid, column of each widget weight...
        for rows in xrange(3):
            Grid.rowconfigure(self, rows, weight=1)
        for columns in xrange(1):
            Grid.columnconfigure(self, columns, weight=1)

        self.label = Label(self, text="This is a test")
        self.label.grid(row=0, column=0, sticky=N+S+E+W)

        self.mytext1 = Text(self, width=30, height=5)
        self.mytext1.grid(row=1, column=0, sticky=N+S+E+W)

        self.mytext2 = Text(self, width=30, height=5)
        self.mytext2.grid(row=2, column=0, sticky=N+S+E+W)


root = Tk()
app = MyFrame(root)
root.mainloop()

你的基本frame看起来像这样:

from Tkinter import *

#Sets up a frame
class MyApplication(Frame):

    #When a class is initialized, this is called as per any class 
    def __init__(self, master):

        #Similar to saying MyFrame = Frame(master)
        Frame.__init__(self, master)

        #Puts the frame on a grid. If you had two frames on one window, you would do the row, column keywords (or not...)
        self.grid()

        #Function to put the widgets on the frame. Can have any name!
        self.create_widgets()

    def create_widgets(self):
        label = Label(self, text='Hello World!')
        label.grid()

        button = Button(self, text='Press Me!', command=self.hello)
        button.grid()

   def hello(self):
        print "Hello World!"

root = Tk()
app = MyApplication(root)
root.mainloop()

任何 Tkinter 小部件都可以这样处理,允许使用模板(我有一个程序,我需要多个具有相同行为的条目(单击清除默认文本,如果未输入任何内容,单击关闭返回它),而不是将绑定添加到每个一个,我能够创建一类行为相同的条目)并且易于实现toplevels(附加窗口)。下面是一个更复杂的程序示例:

class IntroScreen(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.title('Intro Screen')
        self.create_widgets()
        self.focus_force()

    def create_widgets(self):
        label = Label(self, text='Hello World!')
        label.grid()

        button = Button(self, text='Open Window', command=self.newWindow)
        button.grid()

   def newWindow(self):
        self.toplevel = InfoWindow()

#Like the frame, or any widget, this inherited from the parent widget
class InfoWindow(Toplevel):
    def __init__(self):
        Toplevel.__init__(self)
        self.grid()
        self.create_widgets()
        self.focus_force()

    def create_widgets(self):
        label = Label(self, text='This is a window!')
        label.grid()

root = Tk()
app = IntroScreen(root)
root.mainloop()

正如你所看到的,这增加了没有类会更困难的功能。如果您打算进一步了解 Tkinter 的强大功能,请在 stackoverflow 上寻找更多答案(我推荐 Bryan Oakley 的众多信息丰富的答案)并在线进行一些研究!

PS:这是一个很好的起点:在 tkinter 中的两帧之间切换

于 2013-08-15T12:59:38.680 回答
2

这里和上面一样,不使用类。随着程序大小的增加,使用类更简洁,更有意义。请注意,我将标签的权重设置为 0,以防止其扩展。您不必这样做,因为它是默认选项。这只是为了让您了解您可以做什么。

有关网格布局管理器的更多详细信息:http ://www.tkdocs.com/tutorial/grid.html

from Tkinter import *

master = Tk()

Label(master, text="This is a test").grid(row=0, column=0)

mytext1 = Text(master, width=30,height=5)
mytext1.grid(row=1, column=0, sticky="nsew")

mytext2 = Text(master, width=30,height=5)
mytext2.grid(row=2, column=0, sticky="nsew")

master.columnconfigure(0, weight=1)
master.rowconfigure(0, weight=0) # not needed, this is the default behavior
master.rowconfigure(1, weight=1)
master.rowconfigure(2, weight=1)

master.mainloop()
于 2013-08-16T09:47:43.913 回答