将文本放入输入框

2024-05-14 20:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个文本框,其中包括文本框内,但我不断得到一个错误,说条目没有属性集。还有没有办法改变输入框的字体和大小

    HeightEntry = Entry(master, textvariable=Height)
    HeightEntry.pack()
    HeightEntry.set("a default value")
    Height = Height.get()

    Height= StringVar()

Tags: masterdefault属性错误字体条目packentry
1条回答
网友
1楼 · 发布于 2024-05-14 20:25:31

在创建Entry之前先创建StringVar。不要调用Entry.set来设置值,而是调用StringVar.set

HeightVar = StringVar() # now created before creating Entry
HeightEntry = Entry(master, textvariable=HeightVar)
HeightVar.set("a default value") # now called on the StringVar, not on Entry
HeightValue = HeightVar.get() # returns "a default value" from previous line

您也可以在没有StringVar的情况下使用deleteinsert

HeightEntry.delete(0, 'end') # needed only if Entry contains text
HeightEntry.insert(0, 'a default value')

有关更改大小和字体的信息,请参见Fredrik Lundh的The Tkinter Entry Widget或John Shipman的The Entry Widget

相关问题 更多 >

    热门问题