gpt4 book ai didi

python - 如何在 Tkinter 中使用 grid() 制作一个简单的向导?

转载 作者:行者123 更新时间:2023-12-01 04:26:37 25 4
gpt4 key购买 nike

请原谅我。我是 Tkinter 的新手,对 python 只有简单/适度的了解。我的总体目标是制作一个类似向导的程序,随着程序的进行,用户输入不断增加的细节(根据某些输入出现某些框架)。我只是想让核心向导想法发挥作用,但遇到了一些问题。这是我的代码(我已经注释掉了其他来源暗示的事情,这些事情在正确的轨道上,但对我来说失败了):

from Tkinter import *
root = Tk()
#root.minsize(300,300) ?
#root.geometry("300x300") ?

def next():
move(+1)

def prev():
move(-1)

def exit_program():
root.destroy()

page1 = Frame(root, width=300, height=300)
page1.grid()
#page1.grid(row=0,column=0, sticky="nsew") ?
#page1.grid_rowconfigure(0, weight=1) ?
#page1.grid_columnconfigure(0, weight=1) ?
p1_label = Label(page1, text='This is page 1 of the wizard.').grid(column=1, row=0)
p1_quit = Button(page1, text="Quit", command=exit_program).grid(column=1, row=2)
p1_next = Button(page1, text="Next", command=next).grid(column=2, row=2)

page2 = Frame(root)
p2_label = Label(page2, text='This is page 2 of the wizard.').grid(column=1, row=0)
p2_prev = Button(page2, text="Prev", command=prev).grid(column=1, row=2)
p2_next = Button(page2, text="Next", command=next).grid(column=2, row=2)

page3 = Frame(root)
p3_label = Label(page3, text='This is page 3 of the wizard.').grid(column=1, row=0)
p3_prev = Button(page3, text="Prev", command=prev).grid(column=1, row=2)
p3_quit = Button(page3, text="Quit", command=exit_program).grid(column=2, row=2)

pages = [page1, page2, page3]
current = page1
def move(dirn):
global current
idx = pages.index(current) + dirn
if not 0 <= idx < len(pages):
return
current.grid_forget()
current = pages[idx]
current.grid()

root.mainloop()

我有几个问题:

  • 即使我已将宽度和高度设置为一定值,为什么第一个(和所有)框架会缩小到内部小部件的大小?我在网上查看过,显然 grid_propagate(False) 关闭了重新调整大小,但强烈不建议这样做,而且直接不成功(是的,它使窗口达到我想要的尺寸,但“网格”本身仍然位于上部-左边)。我是否应该能够说明框架的尺寸,并且网格将自身与这些尺寸对齐(即保留网格组织,而不是区域)?这个问题与向导进程有关吗?我也不愿意,但我可能仍然可以在一个窗口中编写程序。
  • 为什么网格看起来是 2x2?因为第零个条目有效,所以不应该是 3x3 吗?我在位置 (2x2) 放置了一个按钮 (p1_next)。是因为 0 列中没有任何内容,所以 grid() 将其删除并将所有内容向左移动吗?
  • 这是实现我想要实现的目标的最佳方法吗?恐怕我只知道 OOP 的绝对基础知识,如果可能的话,我宁愿避免(而且这个程序虽然对我来说很大,但按照现代软件开发标准,最终将显得微不足道)。

根据我的学习经验,我有一种感觉,这个问题不是一个快速解决方案,我需要一个大的范式转变才能理解这一点。无论哪种方式,任何帮助将不胜感激

如果这是重复的,我很抱歉。谢谢!

最佳答案

Why does the first (and all) frame(s) shrink to the size of widgets inside, even though I have set the width and height to be a certain amount? ... Shouldn't I be able to state the dimensions of a frame, and the grid aligns itself with those dimensions

这就是 tkinter 的设计工作方式:容器缩小以适应其内容。 99% 的情况下,这是正确的解决方案。正如您所观察到的,您可以关闭几何传播,但这会产生副作用,并且需要您完成通常由 tkinter 更好处理的工作。如果您确实坚持要求帧具有特定大小,通常可以关闭传播。

如果您的具体情况,强制主窗口为特定尺寸比强制框架为特定尺寸更有意义。然后框架将增大(或缩小)以适合主窗口。您可以使用根窗口的geometry 方法来完成此操作。

Why does the grid appear to be 2x2? Shouldn't it be 3x3 because the zeroth entry is valid? I have placed a button (p1_next) in position (2x2). Is it because nothing is in the 0 column, grid() removes it and shifts everything to the left?

如果列中没有任何内容,则该列的默认宽度为零(除非它是统一组的一部分,或者具有最小大小等)。对于行也是如此 - 默认情况下空行的高度为零。

Is this the best approach for what I'm trying to accomplish?

这很难回答。可能没有“最好”的方法。

您可能想查看this answer一个相关的问题,它展示了一种通过堆叠一组框架然后将当前框架提升到堆栈顶部来在一组框架之间切换的方法。

关于python - 如何在 Tkinter 中使用 grid() 制作一个简单的向导?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com