Tkinter window 即使使用 sticky() 和 column configure() 调整大小也不起作用 - raspberry pi 运行 raspbian
Tkinter window resizing does not work even with sticky() and columnconfigure() - rasberry pi running rasbian
我正在尝试制作一个带有按钮和标题的简单 GUI。我目前正在使用 rasbian(linux darbian 的修改版本)在 rasberry pi 运行 上编码。当我尝试调整 window 的大小时,我创建它的行为就像什么都没发生一样消失了,因为 window 太小了。我和其他教程一样。这是我的代码:
content = ttk.Frame(root, padding=(3,3,12,12))
content.grid(column=0, row=0, sticky=(N, S, E, W))
frame = ttk.Frame(content, height=500, width=900)
frame.grid(column=0, row=0, columnspan=3, rowspan=3, sticky=(N, S, E, W))
title = ttk.Label(content, text="QRNG")
title.grid(column=1, row=0, sticky=(N))
diceButton = ttk.Button(content, text="Dice")
diceButton.grid(column=0, row=1, sticky=(W))
imageButton = ttk.Button(content, text="Image")
imageButton.grid(column=1, row=1, sticky=())
randomNumberButton = ttk.Button(content, text="Random Numbers")
randomNumberButton.grid(column=2, row=1, sticky=(E))
fileButton = ttk.Button(content, text="Random Number File")
fileButton.grid(column=0, row=2, sticky=(S, W))
otherButton = ttk.Button(content, text="Empty")
otherButton.grid(column=1, row=2, sticky=(S))
exitButton = ttk.Button(content, text="Exit")
exitButton.grid(column=2, row=2, sticky=(S, E))
root.columnconfigure(0, weight=1, minsize=80)
root.rowconfigure(0, weight=1, minsize=80)
root.columnconfigure(0, weight=1, minsize=80)
root.columnconfigure(1, weight=1, minsize=80)
root.columnconfigure(2, weight=1, minsize=80)
root.rowconfigure(0, weight=1, minsize=80)
root.rowconfigure(1, weight=1, minsize=80)
root.rowconfigure(2, weight=1, minsize=80)
抱歉,大块,但我无法追溯导致此问题的原因。启动 GUI 时粘性起作用,但之后不再起作用并且 GUI 是静态的。更奇怪的是,我从这个网页复制粘贴了这个例子并且它有效但是当我尝试将它应用到我自己的代码时它不起作用。我从this web page的末尾取了代码。
我将非常感谢所提供的任何帮助。以防万一我从按钮中选择了几个选项,我认为这些选项对于理解代码并不重要。
根据经验,每次使用 grid
时,您应该在包含的小部件中至少给一行和一列一个权重。您已经为根小部件完成了该操作,但还没有为 content
或 frame
完成。
另一个问题是您只有根 window 的第 0 行第 0 列中的小部件,但您已经配置了第 0-3 行和第 3 列的权重。
所以,假设要content
填满整个window,去掉根window中第1、2行和第1、2列的配置。
在 content
中,您将小部件放在第 0-3 行和第 0-3 列中。假设您希望这些行和列的内容均匀分布,您应该为每一行和每一列赋予相同的权重。从你的代码来看,你可能正在尝试这样做,但它是在根目录 window 而不是 content
.
上完成的
以下示例包含这些修改。我还重新组织了您的代码以使其更具可读性。根据我的经验,最好将每组小部件的所有布局命令组合在一起。它使最终布局的可视化变得更加容易,并且更容易进行调整。
root.columnconfigure(0, weight=1, minsize=80)
root.rowconfigure(0, weight=1, minsize=80)
content = ttk.Frame(root, padding=(3,3,12,12))
content.grid(column=0, row=0, sticky=(N, S, E, W))
content.rowconfigure(0, weight=1, minsize=80)
content.columnconfigure(0, weight=1, minsize=80)
content.columnconfigure(1, weight=1, minsize=80)
content.columnconfigure(2, weight=1, minsize=80)
content.rowconfigure(1, weight=1, minsize=80)
content.rowconfigure(2, weight=1, minsize=80)
frame = ttk.Frame(content, height=500, width=900)
title = ttk.Label(content, text="QRNG")
diceButton = ttk.Button(content, text="Dice")
imageButton = ttk.Button(content, text="Image")
randomNumberButton = ttk.Button(content, text="Random Numbers")
fileButton = ttk.Button(content, text="Random Number File")
otherButton = ttk.Button(content, text="Empty")
exitButton = ttk.Button(content, text="Exit")
frame.grid(column=0, row=0, columnspan=3, rowspan=3, sticky=(N, S, E, W))
title.grid(column=1, row=0, sticky=(N))
diceButton.grid(column=0, row=1, sticky=(W))
imageButton.grid(column=1, row=1, sticky=())
randomNumberButton.grid(column=2, row=1, sticky=(E))
fileButton.grid(column=0, row=2, sticky=(S, W))
otherButton.grid(column=1, row=2, sticky=(S))
exitButton.grid(column=2, row=2, sticky=(S, E))
我正在尝试制作一个带有按钮和标题的简单 GUI。我目前正在使用 rasbian(linux darbian 的修改版本)在 rasberry pi 运行 上编码。当我尝试调整 window 的大小时,我创建它的行为就像什么都没发生一样消失了,因为 window 太小了。我和其他教程一样。这是我的代码:
content = ttk.Frame(root, padding=(3,3,12,12))
content.grid(column=0, row=0, sticky=(N, S, E, W))
frame = ttk.Frame(content, height=500, width=900)
frame.grid(column=0, row=0, columnspan=3, rowspan=3, sticky=(N, S, E, W))
title = ttk.Label(content, text="QRNG")
title.grid(column=1, row=0, sticky=(N))
diceButton = ttk.Button(content, text="Dice")
diceButton.grid(column=0, row=1, sticky=(W))
imageButton = ttk.Button(content, text="Image")
imageButton.grid(column=1, row=1, sticky=())
randomNumberButton = ttk.Button(content, text="Random Numbers")
randomNumberButton.grid(column=2, row=1, sticky=(E))
fileButton = ttk.Button(content, text="Random Number File")
fileButton.grid(column=0, row=2, sticky=(S, W))
otherButton = ttk.Button(content, text="Empty")
otherButton.grid(column=1, row=2, sticky=(S))
exitButton = ttk.Button(content, text="Exit")
exitButton.grid(column=2, row=2, sticky=(S, E))
root.columnconfigure(0, weight=1, minsize=80)
root.rowconfigure(0, weight=1, minsize=80)
root.columnconfigure(0, weight=1, minsize=80)
root.columnconfigure(1, weight=1, minsize=80)
root.columnconfigure(2, weight=1, minsize=80)
root.rowconfigure(0, weight=1, minsize=80)
root.rowconfigure(1, weight=1, minsize=80)
root.rowconfigure(2, weight=1, minsize=80)
抱歉,大块,但我无法追溯导致此问题的原因。启动 GUI 时粘性起作用,但之后不再起作用并且 GUI 是静态的。更奇怪的是,我从这个网页复制粘贴了这个例子并且它有效但是当我尝试将它应用到我自己的代码时它不起作用。我从this web page的末尾取了代码。 我将非常感谢所提供的任何帮助。以防万一我从按钮中选择了几个选项,我认为这些选项对于理解代码并不重要。
根据经验,每次使用 grid
时,您应该在包含的小部件中至少给一行和一列一个权重。您已经为根小部件完成了该操作,但还没有为 content
或 frame
完成。
另一个问题是您只有根 window 的第 0 行第 0 列中的小部件,但您已经配置了第 0-3 行和第 3 列的权重。
所以,假设要content
填满整个window,去掉根window中第1、2行和第1、2列的配置。
在 content
中,您将小部件放在第 0-3 行和第 0-3 列中。假设您希望这些行和列的内容均匀分布,您应该为每一行和每一列赋予相同的权重。从你的代码来看,你可能正在尝试这样做,但它是在根目录 window 而不是 content
.
以下示例包含这些修改。我还重新组织了您的代码以使其更具可读性。根据我的经验,最好将每组小部件的所有布局命令组合在一起。它使最终布局的可视化变得更加容易,并且更容易进行调整。
root.columnconfigure(0, weight=1, minsize=80)
root.rowconfigure(0, weight=1, minsize=80)
content = ttk.Frame(root, padding=(3,3,12,12))
content.grid(column=0, row=0, sticky=(N, S, E, W))
content.rowconfigure(0, weight=1, minsize=80)
content.columnconfigure(0, weight=1, minsize=80)
content.columnconfigure(1, weight=1, minsize=80)
content.columnconfigure(2, weight=1, minsize=80)
content.rowconfigure(1, weight=1, minsize=80)
content.rowconfigure(2, weight=1, minsize=80)
frame = ttk.Frame(content, height=500, width=900)
title = ttk.Label(content, text="QRNG")
diceButton = ttk.Button(content, text="Dice")
imageButton = ttk.Button(content, text="Image")
randomNumberButton = ttk.Button(content, text="Random Numbers")
fileButton = ttk.Button(content, text="Random Number File")
otherButton = ttk.Button(content, text="Empty")
exitButton = ttk.Button(content, text="Exit")
frame.grid(column=0, row=0, columnspan=3, rowspan=3, sticky=(N, S, E, W))
title.grid(column=1, row=0, sticky=(N))
diceButton.grid(column=0, row=1, sticky=(W))
imageButton.grid(column=1, row=1, sticky=())
randomNumberButton.grid(column=2, row=1, sticky=(E))
fileButton.grid(column=0, row=2, sticky=(S, W))
otherButton.grid(column=1, row=2, sticky=(S))
exitButton.grid(column=2, row=2, sticky=(S, E))