问题描述
限时送ChatGPT账号..我正在努力获取一个 python/tkinter 标签小部件来更新其内容.根据今天早些时候的一个线程,我按照有关如何组合小部件的说明进行操作.但是,在运行时,当我单击按钮 Calculeaza 时,标签小部件不会更改内容.据我所知,函数 Calculeaza() 是错误的.
I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, when I click button Calculeaza. As far as I can tell, function Calculeaza() is wrong.
def Calculeaza(self):
cgrade =celsiusEntry.get()
if cgrade == ' ':
fahrenheitEntry.configure(text = ' ')
else:
cgrade=float(cgrade)
fgrade=(cgrade-32)/1.8
fahrenheitEntry.configure(text=str(fgrade))# is not function
这是代码:
import sys
from Tkinter import *
class C2F(Frame):
#celsiusEntry = Entry
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Convertor Celsius/Fahrenheit")
self.pack(fill=BOTH, expand=1)
# Meniul superior cu File>Exit si Help>About
menuBar= Menu(self.parent)
self.parent.config(menu=menuBar)
fileMenu= Menu(menuBar)
fileMenu.add_command(label="Exit", command = self.onExit)
menuBar.add_cascade(label="File", menu=fileMenu)
# Adaugare butoane http://effbot/tkinterbook/grid.htm
"""
Label(self.parent, text="First").grid(row=0, column =0)
Label(self.parent, text="First").grid(row=1, column = 0)
"""
labelframe = LabelFrame(self.parent, text="Celsius/Fahrenheit")
labelframe.pack(fill="both", expand="yes")
left = Label(labelframe, text="Celsius")
left.grid(row=0, column=0)
Label(labelframe, text="Fahrenheit").grid(row=1, column =0)
global celsiusEntry
celsiusEntry=Entry(labelframe, bd=5)
celsiusEntry.grid(row=0, column=1)
global fahrenheitEntry
fahrenheitEntry=Entry(labelframe, bd=5, text="salut")
fahrenheitEntry.grid(row=1, column=1)
calcButon = Button(labelframe, text="Calculeaza", command=self.Calculeaza)
calcButon.grid(row=1, column=2)
def onExit(self):
self.parent.quit()
def Calculeaza(self):
cgrade =celsiusEntry.get()
if cgrade == ' ':
fahrenheitEntry.configure(text = ' ')
else:
cgrade=float(cgrade)
fgrade=(cgrade-32)/1.8
fahrenheitEntry.config(text=str(fgrade))# is not function
def main():
root= Tk()
root.geometry("350x350+300+300")
app= C2F(root)
#Label(root, text="First").grid(row=0, column =0)
root.mainloop()
if __name__ == "__main__":
main()
推荐答案
条目小部件没有 set
方法.如果您正在阅读的某些文档表明确实如此,您可能需要联系作者以告诉他们他们是正确的.
Entry widgets don't have a set
method. If there is some documentation you are reading that says it does, you might want to contact the author to tell them they are correct.
对于条目小部件,您有两种选择.一,如果你有一个 textvariable
关联,你可以在 textvariable 上调用 set
.这将导致与 textvariable
关联的任何小部件被更新.其次,如果没有 textvariable
,您可以使用 insert
和 delete
方法来替换小部件中的内容.
For an entry widget you have two choices. One, if you have a textvariable
associated, you can call set
on the textvariable. This will cause any widgets associated with the textvariable
to be updated. Second, without a textvariable
you can use the insert
and delete
methods to replace what is in the widget.
这是后者的一个例子:
fahrenheitEntry.delete(0, "end")
fahrenheitEntry.insert(0, cgrade)
这篇关于如何在 Python 中将特定文本设置为 Entry?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论