如何制作可以在 Tkinter 中调整大小的框架
How To Make A Frame That Can Be Resized in Tkinter
我正在使用 Tkinter 并且有一个框架需要重新调整,这是否可以在不改变 class 或网格管理器工作方式的情况下完成(继承框架方法并使用框架作为我的window)?我尝试查看其他 Stack Overflow 问答,但不太理解它们,因为每个问题都有其他代码片段干扰了一般问题。
代码:
from tkinter import *
class Window(Frame): # Inherits Frame methods
def __init__(self, master):
Frame.__init__(self, master, bg='LightBlue') # Initializes the frame
self.master = master # Creates master
self.init_window() # Method to grid
self.master.mainloop() # Runs window
def init_window(self):
self.grid()
root = Tk() # Creates a master
app = Window(root) # Initializes app using root as master
有人可以帮我解决这个问题吗?
编辑:
为什么 Novel 的答案有效而不是...
代码:
from tkinter import *
class Window(Frame): # Inherits Frame methods
def __init__(self, master):
Frame.__init__(self, master, bg='LightBlue') # Initializes the frame
self.master = master # Creates master
self.init_window() # Method to grid
self.master.mainloop() # Runs window
def init_window(self):
self.grid(column=0, row=0, sticky=N+S+E+W)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
root = Tk() # Creates a master
app = Window(root) # Initializes app using root as master
有人能解释一下吗?
您需要使用 columnconfigure 和 rowconfigure 来设置网格列和行以随 window 展开。另外:
- 不要使用通配符导入
- 使用
sticky
参数使小部件的大小与网格单元格保持一致
- 不要在同一框架内布局框架。 IOW 不要使用
self.grid()
,总是初始化并在外部使用 instance.grid()
。
- 请务必在任何时候将 tkinter 小部件子类化时传递 kwargs
- 查看更多面向初学者的论坛,例如 learnpython.reddit.com
所有这些加在一起:
import tkinter as tk
class Window(tk.Frame): # Inherits Frame methods
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, bg='LightBlue', **kwargs) # Initializes the frame
root = tk.Tk() # Creates a master
root.geometry('200x200') # set initial size
app = Window(root) # Initializes app using root as master
app.grid(sticky='nsew') # set this widget to keep the size of the grid cell
root.columnconfigure(0, weight=1) # Set the first column to expand with the window
root.rowconfigure(0, weight=1) # Set the first row to expand with the window
root.mainloop() # Runs window
您也可以使用 pack
用更少的代码做同样的事情:
import tkinter as tk
class Window(tk.Frame): # Inherits Frame methods
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, bg='LightBlue', **kwargs) # Initializes the frame
root = tk.Tk() # Creates a master
root.geometry('200x200') # set initial size
app = Window(root) # Initializes app using root as master
app.pack(fill=tk.BOTH, expand=True) # set this widget to fill all available space
root.mainloop() # Runs window
重新编辑:
您需要在 Widget 的 master 上设置 rowconfigure,而不是 Widget 本身。 Widget得到多少space由master决定。因此,您可以根据需要使用 self.master.rowconfigure(0, weight=1),但我展示的结构更适合阅读代码。如果您从子 Widget 中修改主 Widget,代码会变得混乱。
我正在使用 Tkinter 并且有一个框架需要重新调整,这是否可以在不改变 class 或网格管理器工作方式的情况下完成(继承框架方法并使用框架作为我的window)?我尝试查看其他 Stack Overflow 问答,但不太理解它们,因为每个问题都有其他代码片段干扰了一般问题。
代码:
from tkinter import *
class Window(Frame): # Inherits Frame methods
def __init__(self, master):
Frame.__init__(self, master, bg='LightBlue') # Initializes the frame
self.master = master # Creates master
self.init_window() # Method to grid
self.master.mainloop() # Runs window
def init_window(self):
self.grid()
root = Tk() # Creates a master
app = Window(root) # Initializes app using root as master
有人可以帮我解决这个问题吗?
编辑:
为什么 Novel 的答案有效而不是...
代码:
from tkinter import *
class Window(Frame): # Inherits Frame methods
def __init__(self, master):
Frame.__init__(self, master, bg='LightBlue') # Initializes the frame
self.master = master # Creates master
self.init_window() # Method to grid
self.master.mainloop() # Runs window
def init_window(self):
self.grid(column=0, row=0, sticky=N+S+E+W)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
root = Tk() # Creates a master
app = Window(root) # Initializes app using root as master
有人能解释一下吗?
您需要使用 columnconfigure 和 rowconfigure 来设置网格列和行以随 window 展开。另外:
- 不要使用通配符导入
- 使用
sticky
参数使小部件的大小与网格单元格保持一致 - 不要在同一框架内布局框架。 IOW 不要使用
self.grid()
,总是初始化并在外部使用instance.grid()
。 - 请务必在任何时候将 tkinter 小部件子类化时传递 kwargs
- 查看更多面向初学者的论坛,例如 learnpython.reddit.com
所有这些加在一起:
import tkinter as tk
class Window(tk.Frame): # Inherits Frame methods
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, bg='LightBlue', **kwargs) # Initializes the frame
root = tk.Tk() # Creates a master
root.geometry('200x200') # set initial size
app = Window(root) # Initializes app using root as master
app.grid(sticky='nsew') # set this widget to keep the size of the grid cell
root.columnconfigure(0, weight=1) # Set the first column to expand with the window
root.rowconfigure(0, weight=1) # Set the first row to expand with the window
root.mainloop() # Runs window
您也可以使用 pack
用更少的代码做同样的事情:
import tkinter as tk
class Window(tk.Frame): # Inherits Frame methods
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, bg='LightBlue', **kwargs) # Initializes the frame
root = tk.Tk() # Creates a master
root.geometry('200x200') # set initial size
app = Window(root) # Initializes app using root as master
app.pack(fill=tk.BOTH, expand=True) # set this widget to fill all available space
root.mainloop() # Runs window
重新编辑:
您需要在 Widget 的 master 上设置 rowconfigure,而不是 Widget 本身。 Widget得到多少space由master决定。因此,您可以根据需要使用 self.master.rowconfigure(0, weight=1),但我展示的结构更适合阅读代码。如果您从子 Widget 中修改主 Widget,代码会变得混乱。