【问题标题】:Tkinter window geometryTkinter 窗口几何
【发布时间】:2021-10-07 03:28:30
【问题描述】:

我正在使用 Tkinter 窗口,我想在没有几何功能的情况下设置屏幕的高度和宽度,所以我制作了 2 个垂直和水平刻度,然后我使用一个带有命令的按钮来设置以下代码中的窗口大小:

root = Tk()
root.title("Root window")

label = Label(root, text="The height").pack()
slay = Scale(root, from_=0, to=200)

slay.pack()

my_label = Label(root, text="The width").pack()

hor = Scale(root, from_=0, to=250, orient=HORIZONTAL)

hor.pack()

def btns():
 root.geometry(str(f"{slay.get} x {hor.get}"))

btn = Button(root, text="Setup the window size", command=btns).pack()

错误是: 第 20 行,在 btns 中 root.geometry(str(f"{slay.get} x {hor.get}"))

【问题讨论】:

  • btns 使用两个步骤。首先,计算字符串并将其保存在变量中,然后将其传递给geometry。当你这样做时,你将能够看到你传递给geometry 的内容。它不会是你假设的那样。

标签: python tkinter


【解决方案1】:

您只是在引用.get() 函数,从未真正调用它们。所以你会收到一个错误。

使用() 调用函数。另外str(f"{slay.get()} x {hor.get()}")之间没有空格。

所以它看起来像这样:

root.geometry(str(f"{slay.get()}x{hor.get()}"))

【讨论】:

  • 谢谢你的回答,但是我用()试了好几次都不行,试试吧
  • 再次谢谢你,但我知道该怎么做是这样的:root.geometry((str(hor.get()) + "x" + str(slay.get()) ))
【解决方案2】:

这行得通。

问题是root.geometry(str(f"{vert.get} x {hori.get}"))

我改成这个root.geometry(str(f"{vert.get()}x{hori.get()}"))

只需删除“x”周围的空格并将() 添加到get

还添加了 orient = VERTICAL 来杀死

root = Tk()
root.title("Root window")

label = Label(root, text="The height").pack()
slay = Scale(root, from_=0, to=200, orient=VERTICAL)

slay.pack()

my_label = Label(root, text="The width").pack()

hor = Scale(root, from_=0, to=250, orient=HORIZONTAL)

hor.pack()

def btns():
    print( slay.get(), hor.get() )
    root.geometry(str(f"{slay.get()}x{hor.get()}"))

btn = Button(root, text="Setup the window size", command=btns).pack()

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-09
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2010-09-11
    • 2018-05-30
    相关资源
    最近更新 更多