如何为我的程序创建一个与以下代码相关的重置按钮? [英] How would I create a reset button for my program relating with the following code?
问题描述
我正在尝试添加一个重置按钮,但似乎无法正常工作.我创建了一个主目录,以便在按下按钮但没有运气时可以参考它.有什么想法吗?
I am trying to add a reset button but I can't seem to get it to work. I created a main in order to refer back to it when the button is pressed but no luck. Any ideas?
import sys
from tkinter import *
import math
def main():
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)
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 = main).place(x=10,y=165)
quit = Button(text="Quit", command = closeWin).place(x=385,y=165)
myGui.mainloop()
main()
推荐答案
再次调用 main()
,您只是在创建GUI的另一个实例.相反,您应该做的是(如果我理解正确的话),重置当前现有GUI的值.您可以使用GUI对象的 set()
方法.
By calling main()
again, you are simply creating another instance of the GUI. What you should do instead is (if I understand correctly), reset the values of the currently existing GUI. You can use the set()
method of the GUI objects.
是
def reset_values():
kiloMent.set(0)
mileMent.set(0)
reset = Button(text="Reset Values!", command=reset_values).place(x=10, y=165)
把戏吗?
更彻底地查看您的代码,但是,那里还存在其他一些问题.首先,我建议不要在用户每次尝试转换值时都创建一个 Label
.
Looking at your code more thoroughly, however, there are some other problems there, as well. To start with, I would suggest not creating a Label
everytime the user tries to convert a value.
这篇关于如何为我的程序创建一个与以下代码相关的重置按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!