加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:检索ttk.Frame大小

发布时间:2020-12-20 12:03:04 所属栏目:Python 来源:网络整理
导读:我想要获得Frame的大小 – 但没有运气. 在下面的代码中 – 我得到了两个答案,“0”(标有***的行) from tkinter import *from tkinter import ttkroot = Tk()w = '400'h = '100'root.geometry('{}x{}'.format(w,h))root.configure(bg='lightgreen')txt = Stri
我想要获得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,column=0)

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

root.mainloop()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读