Python : Retrieve ttk. 帧大小

标签 python tkinter

我想获得 Frame 的尺寸 - 但运气不好。

在下面的代码中 - 我得到了两个答案,“0”(标有 *** 的行)

from tkinter import *
from tkinter import ttk

root = Tk()
w = '400'
h = '100'
root.geometry('{}x{}'.format(w, h))
root.configure(bg='lightgreen')

txt = StringVar()
txt.set("Hello")


testframe = ttk.Frame(root)
testframe.grid(row=0, column=1 )

label1 = ttk.Label(testframe, textvariable=txt)
label1.grid(row=0, column=0)

print(testframe["width"], testframe.cget("width"))   ***This line

root.mainloop()

最佳答案

需要先调用update 方法,以便呈现小部件。 如果不是,则其宽度等于零。 这在 this answer 中有解释。 .

现在,testframe['width'] 的问题在于它只是小部件实际宽度的提示,如this answer 中所述。 .

以下代码将按预期输出 32

from tkinter import *
from tkinter import ttk

root = Tk()
w = '400'
h = '100'
root.geometry('{}x{}'.format(w, h))
root.configure(bg='lightgreen')

txt = StringVar()
txt.set("Hello")


testframe = ttk.Frame(root)
testframe.grid(row=0, column=1 )

label1 = ttk.Label(testframe, textvariable=txt)
label1.grid(row=0, column=0)

# Modified lines
testframe.update()
print(testframe.winfo_width())

root.mainloop()

关于Python : Retrieve ttk. 帧大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46071765/

相关文章:

python - 通过按 'Enter' 键而不是单击按钮来获取值

python - 如何对齐 tkinter 小部件?

python - 评估 Pandas DataFrame 中的 bool 表达式

python - 将训练数据添加到现有的 LinearSVC

python - 在Kubernetes中容器化Flask微服务

python - Python 中的可编辑组合框

python - 将 kwargs 传递给 re.sub()

Python,tictactoe,语法错误

python - 如何在 TKinter 列表框中移动选择多个项目?

python - 使用用户输入更新 Matplotlib 图