为什么在按下 "Reset" 按钮时我的标签无法清除?
Why can't I get my labels to clear when the "Reset" button is pressed?
我正在制作一个程序,将公里转换为英里,将英里转换为公里。当显示值时,我希望有一个重置按钮来重置值和用户编号。
我可以输入要清除的数字,但不能输入标签(finalKilo、finalMile)。有什么建议吗?
此外,有没有其他方法可以不使用标签来显示计算值?
from tkinter import *
def closeWin():
myGui.destroy() #Close Window Function
def kiloFunc():
myText = kiloMent.get() #Kilometers to Miles Fuction
convert = 0.62
miles = myText * convert
finalKilo = Label(text = miles,fg='red',justify='center').place(x=200,y=80)
def mileFunc():
myText2 = mileMent.get() #Miles to Kilometers Function
convertTwo = myText2 // 0.62
finalMile = Label(text = convertTwo, fg = 'red',justify='center').place(x=200,y=170)
def reset_values():
kiloMent.set(0)
mileMent.set(0) #Resets the Data
finalKilo.set("")
finalMile.set("")
#The GUI
myGui = Tk()
kiloMent = IntVar()
mileMent = IntVar()
myGui.title("Distance Converter")
myGui.geometry("450x200+500+200")
myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
myLabel.pack()
kiloEntry = Entry(myGui, textvariable = kiloMent,justify='center').pack()
kilo2milesButton = Button(text = "Kilometers to Miles!", command = kiloFunc).pack()
mileEntry = Entry(myGui, textvariable = mileMent,justify='center').place(x=130,y=105)
miles2kiloButton = Button(text = "Miles to Kilometers!", command = mileFunc).place(x=150,y=135)
reset = Button(text = "Reset Values!", command = reset_values).place(x=10,y=165)
quit = Button(text="Quit", command = closeWin).place(x=385,y=165)
myGui.mainloop()
与其在每次转换上创建新标签,不如为结果创建一个标签,而不是 text
,为其分配一个 textvariable
,它是 [=12= 的一个实例].在该变量上,您可以调用 set
来更改与该变量关联的标签的文本,
Callidior 是正确的,有多个错误:)
我已经调整了代码,将它放在 class 中,现在它应该可以工作了。
现在它在尝试更改标签时引用实际变量(miles 和 convertTwo)。您提到了标签本身。在那种情况下,您可能应该做类似的事情:
finalKilo.config(text = "")
finalMile.config(text = "")
另一个问题是您仅在输入条目后才定义标签,而在重置时您将两者都重置了。因此,如果第一次只给出一个条目并按下重置,则会弹出一条错误消息,指出其中一个标签不是 defined/None.
下面的代码应该可以工作。
from Tkinter import *
class hisGui:
def __init__(self):
self.myGui = Tk()
self.kiloMent = IntVar()
self.mileMent = IntVar()
self.myGui.title("Distance Converter")
self.myGui.geometry("450x200+500+200")
myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
myLabel.pack()
self.miles = StringVar()
self.convertTwo = StringVar()
self.finalKilo = Label(textvariable = self.miles,fg='red',justify='center').place(x=200,y=80)
self.finalMile = Label(textvariable = self.convertTwo, fg = 'red',justify='center').place(x=200,y=170)
kiloEntry = Entry(self.myGui, textvariable = self.kiloMent,justify='center').pack()
kilo2milesButton = Button(text = "Kilometers to Miles!", command = self.kiloFunc).pack()
mileEntry = Entry(self.myGui, textvariable = self.mileMent,justify='center').place(x=130,y=105)
miles2kiloButton = Button(text = "Miles to Kilometers!", command = self.mileFunc).place(x=150,y=135)
reset = Button(text = "Reset Values!", command = self.reset_values).place(x=10,y=165)
quit = Button(text="Quit", command = self.closeWin).place(x=385,y=165)
self.myGui.mainloop()
def closeWin(self):
self.myGui.destroy() #Close Window Function
def kiloFunc(self):
myText = self.kiloMent.get() #Kilometers to Miles Function
convert = 0.62
self.miles.set(myText * convert)
def mileFunc(self):
myText2 = self.mileMent.get() #Miles to Kilometers Function
self.convertTwo.set(myText2 // 0.62)
def reset_values(self):
self.kiloMent.set(0)
self.mileMent.set(0) #Resets the Data
self.miles.set("")
self.convertTwo.set("")
if __name__ == '__main__':
Gui = hisGui()
我正在制作一个程序,将公里转换为英里,将英里转换为公里。当显示值时,我希望有一个重置按钮来重置值和用户编号。
我可以输入要清除的数字,但不能输入标签(finalKilo、finalMile)。有什么建议吗?
此外,有没有其他方法可以不使用标签来显示计算值?
from tkinter import *
def closeWin():
myGui.destroy() #Close Window Function
def kiloFunc():
myText = kiloMent.get() #Kilometers to Miles Fuction
convert = 0.62
miles = myText * convert
finalKilo = Label(text = miles,fg='red',justify='center').place(x=200,y=80)
def mileFunc():
myText2 = mileMent.get() #Miles to Kilometers Function
convertTwo = myText2 // 0.62
finalMile = Label(text = convertTwo, fg = 'red',justify='center').place(x=200,y=170)
def reset_values():
kiloMent.set(0)
mileMent.set(0) #Resets the Data
finalKilo.set("")
finalMile.set("")
#The GUI
myGui = Tk()
kiloMent = IntVar()
mileMent = IntVar()
myGui.title("Distance Converter")
myGui.geometry("450x200+500+200")
myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
myLabel.pack()
kiloEntry = Entry(myGui, textvariable = kiloMent,justify='center').pack()
kilo2milesButton = Button(text = "Kilometers to Miles!", command = kiloFunc).pack()
mileEntry = Entry(myGui, textvariable = mileMent,justify='center').place(x=130,y=105)
miles2kiloButton = Button(text = "Miles to Kilometers!", command = mileFunc).place(x=150,y=135)
reset = Button(text = "Reset Values!", command = reset_values).place(x=10,y=165)
quit = Button(text="Quit", command = closeWin).place(x=385,y=165)
myGui.mainloop()
与其在每次转换上创建新标签,不如为结果创建一个标签,而不是 text
,为其分配一个 textvariable
,它是 [=12= 的一个实例].在该变量上,您可以调用 set
来更改与该变量关联的标签的文本,
Callidior 是正确的,有多个错误:)
我已经调整了代码,将它放在 class 中,现在它应该可以工作了。
现在它在尝试更改标签时引用实际变量(miles 和 convertTwo)。您提到了标签本身。在那种情况下,您可能应该做类似的事情:
finalKilo.config(text = "")
finalMile.config(text = "")
另一个问题是您仅在输入条目后才定义标签,而在重置时您将两者都重置了。因此,如果第一次只给出一个条目并按下重置,则会弹出一条错误消息,指出其中一个标签不是 defined/None.
下面的代码应该可以工作。
from Tkinter import *
class hisGui:
def __init__(self):
self.myGui = Tk()
self.kiloMent = IntVar()
self.mileMent = IntVar()
self.myGui.title("Distance Converter")
self.myGui.geometry("450x200+500+200")
myLabel = Label(text="Welcome! Please enter your value then choose your option:",fg="blue",justify='center')
myLabel.pack()
self.miles = StringVar()
self.convertTwo = StringVar()
self.finalKilo = Label(textvariable = self.miles,fg='red',justify='center').place(x=200,y=80)
self.finalMile = Label(textvariable = self.convertTwo, fg = 'red',justify='center').place(x=200,y=170)
kiloEntry = Entry(self.myGui, textvariable = self.kiloMent,justify='center').pack()
kilo2milesButton = Button(text = "Kilometers to Miles!", command = self.kiloFunc).pack()
mileEntry = Entry(self.myGui, textvariable = self.mileMent,justify='center').place(x=130,y=105)
miles2kiloButton = Button(text = "Miles to Kilometers!", command = self.mileFunc).place(x=150,y=135)
reset = Button(text = "Reset Values!", command = self.reset_values).place(x=10,y=165)
quit = Button(text="Quit", command = self.closeWin).place(x=385,y=165)
self.myGui.mainloop()
def closeWin(self):
self.myGui.destroy() #Close Window Function
def kiloFunc(self):
myText = self.kiloMent.get() #Kilometers to Miles Function
convert = 0.62
self.miles.set(myText * convert)
def mileFunc(self):
myText2 = self.mileMent.get() #Miles to Kilometers Function
self.convertTwo.set(myText2 // 0.62)
def reset_values(self):
self.kiloMent.set(0)
self.mileMent.set(0) #Resets the Data
self.miles.set("")
self.convertTwo.set("")
if __name__ == '__main__':
Gui = hisGui()