如何为我的程序创建一个与以下代码相关的重置按钮?

时间:2016-03-14 18:04:47

标签: python button tkinter reset

我正在尝试添加重置按钮,但我似乎无法让它工作。我创建了一个main,以便在按下按钮时返回它,但没有运气。有什么想法吗?

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()

2 个答案:

答案 0 :(得分:3)

再次调用main(),您只需创建另一个GUI实例。你应该做的是(如果我理解正确的话),重置当前现有GUI的值。您可以使用GUI对象的set()方法。

确实

def reset_values():
    kiloMent.set(0)
    mileMent.set(0)
reset = Button(text="Reset Values!", command=reset_values).place(x=10, y=165)

诀窍?

然而,更彻底地查看您的代码,还有其他一些问题。首先,我建议每次用户尝试转换值时都不要创建Label

答案 1 :(得分:1)

此代码应该有效:

from tkinter import *

def main():
    def closeWin():
        myGui.destroy()  # Close Window Function

    def kiloFunc():
        finalKilo.set(kiloMent.get() * 0.62)  # Kilometers to Miles Fuction

    def mileFunc():
        finalMile.set(mileMent.get() // 0.62)  # Miles to Kilometers Function

    def clearFunc():
        kiloMent.set("0")
        mileMent.set("0")
        finalKilo.set("")
        finalMile.set("")

    myGui = Tk()

    kiloMent = IntVar()
    mileMent = IntVar()

    finalKilo = StringVar()
    finalMile = StringVar()

    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')
    kiloEntry.pack()

    kilo2milesButton = Button(text="Kilometers to Miles!", command=kiloFunc)
    kilo2milesButton.pack()

    mileEntry = Entry(myGui, textvariable=mileMent, justify='center')
    mileEntry.place(x=130, y=105)

    miles2kiloButton = Button(text="Miles to Kilometers!", command=mileFunc)
    miles2kiloButton.place(x=150, y=135)

    kiloLabel = Label(textvariable=finalKilo, fg='red', justify='center')
    kiloLabel.place(x=200, y=80)

    mileLabel = Label(textvariable=finalMile, fg='red', justify='center')
    mileLabel.place(x=200, y=170)

    reset = Button(text="Reset Values!", command=clearFunc)
    reset.place(x=10, y=165)

    quit = Button(text="Quit", command=closeWin)
    quit.place(x=385, y=165)

    myGui.mainloop()

main()

除了Chuck提到的原始代码之外,还有一些关于原始代码的注释:

  1. mathsys导入未使用。
  2. 您设置的变量等于widget.pack()widget.place(),它们是返回None的函数。