Python 调整窗口大小不需要';t调整tkinter中的内容大小
我正在制作我的第一个GUI应用程序,我遇到了一个愚蠢的问题。调整主窗口的大小不会调整其内容的大小,并且会留下空白。我读过TKDocs,他们只说应该使用sticky和列/行权重属性,但我真的不明白它们是如何工作的。 以下是我的代码(仅包括小部件的部分,如果您认为问题不在这里,我将发布其余部分):Python 调整窗口大小不需要';t调整tkinter中的内容大小,python,tkinter,tk,Python,Tkinter,Tk,我正在制作我的第一个GUI应用程序,我遇到了一个愚蠢的问题。调整主窗口的大小不会调整其内容的大小,并且会留下空白。我读过TKDocs,他们只说应该使用sticky和列/行权重属性,但我真的不明白它们是如何工作的。 以下是我的代码(仅包括小部件的部分,如果您认为问题不在这里,我将发布其余部分): 谢谢你的时间 也许这会帮你找到正确的方向。确保在每个级别配置列/行权重 import tkinter.ttk from tkinter.constants import * class Applicat
谢谢你的时间 也许这会帮你找到正确的方向。确保在每个级别配置列/行权重
import tkinter.ttk
from tkinter.constants import *
class Application(tkinter.ttk.Frame):
@classmethod
def main(cls):
tkinter.NoDefaultRoot()
root = tkinter.Tk()
app = cls(root)
app.grid(sticky=NSEW)
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.resizable(True, False)
root.mainloop()
def __init__(self, root):
super().__init__(root)
self.create_variables()
self.create_widgets()
self.grid_widgets()
self.grid_columnconfigure(0, weight=1)
def create_variables(self):
self.player1 = tkinter.StringVar(self, 'Player 1')
self.player2 = tkinter.StringVar(self, 'Player 2')
self.timer = tkinter.StringVar(self)
self.running = tkinter.BooleanVar(self)
def create_widgets(self):
self.set_timer = tkinter.ttk.Entry(self, textvariable=self.timer)
self.start = tkinter.ttk.Button(self, text='Start', command=self.start)
self.display1 = tkinter.ttk.Label(self, textvariable=self.player1)
self.display2 = tkinter.ttk.Label(self, textvariable=self.player2)
def grid_widgets(self):
options = dict(sticky=NSEW, padx=3, pady=4)
self.set_timer.grid(column=0, row=0, **options)
self.start.grid(column=0, row=1, **options)
self.display1.grid(column=0, row=2, **options)
self.display2.grid(column=0, row=3, **options)
def start(self):
timer = self.timer.get()
self.player1.set(timer)
self.player2.set(timer)
if __name__ == '__main__':
Application.main()
好的,谢谢,我有点希望我能避免上课,他们吓到我了。但我想这一切都是第一次…@Dunno:如果你需要
tkinter
应用程序使用类的例子,请告诉我。当我第一次开始使用GUI库时,我的代码与您的类似。现在每当我注意到这样的代码时,我的第一个想法是代码需要完全重写。谢谢你的提议,但是互联网上充满了这样的例子,我不想占用你太多的时间来练习我的google fu;)根据,您可能会发现这是一个有用的参考。
import tkinter.ttk
from tkinter.constants import *
class Application(tkinter.ttk.Frame):
@classmethod
def main(cls):
tkinter.NoDefaultRoot()
root = tkinter.Tk()
app = cls(root)
app.grid(sticky=NSEW)
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.resizable(True, False)
root.mainloop()
def __init__(self, root):
super().__init__(root)
self.create_variables()
self.create_widgets()
self.grid_widgets()
self.grid_columnconfigure(0, weight=1)
def create_variables(self):
self.player1 = tkinter.StringVar(self, 'Player 1')
self.player2 = tkinter.StringVar(self, 'Player 2')
self.timer = tkinter.StringVar(self)
self.running = tkinter.BooleanVar(self)
def create_widgets(self):
self.set_timer = tkinter.ttk.Entry(self, textvariable=self.timer)
self.start = tkinter.ttk.Button(self, text='Start', command=self.start)
self.display1 = tkinter.ttk.Label(self, textvariable=self.player1)
self.display2 = tkinter.ttk.Label(self, textvariable=self.player2)
def grid_widgets(self):
options = dict(sticky=NSEW, padx=3, pady=4)
self.set_timer.grid(column=0, row=0, **options)
self.start.grid(column=0, row=1, **options)
self.display1.grid(column=0, row=2, **options)
self.display2.grid(column=0, row=3, **options)
def start(self):
timer = self.timer.get()
self.player1.set(timer)
self.player2.set(timer)
if __name__ == '__main__':
Application.main()