【问题标题】:Temperature Converter with GUI issues有 GUI 问题的温度转换器
【发布时间】:2017-04-25 17:57:12
【问题描述】:

这是一个带有 GUI 的温度转换器。我在 GUI 方面遇到问题。在我添加到 GUI 中之前,我的代码运行正确,现在说它已全部检出,但当我实际运行程序时什么都不会发生。我不知道是不是因为我可能需要 Tkinter 文件在同一个文件夹中?我之前从文件中获取文本或者我的 GUI 完全错误地编程时遇到了这个问题!谢谢!

#import
#main function
from Tkinter import *
def main():
    root=Tk()

    root.title("Temperature Converter")
    root.geometry("400x700")
    #someothersting=""
    someotherstring=""
#enter Celcius
    L1=Label(root,text="Enter a Celcius temperature.")
    E1=Entry(root,textvariable=someotherstring)
    somebutton=Button(root, text="Total", command=lambda: convert(E1.get()))

    somebutton.pack()
    E1.pack()
    L1.pack()
    root.mainloop()#main loop


#convert Celcius to Fahrenheit
def convert(somestring):
    if somestring != "":    
        # cel=0 dont need these in python
        # far=0
        cel=int(somestring)
        far=(9/5*(cel))+32
        print(F)

【问题讨论】:

  • 它也在 python 2 中
  • 许多语言会自动为您调用main,但Python 不是其中之一。尝试在末尾添加main()

标签: python tkinter series


【解决方案1】:

添加 main() 并将 F 更改为 far。我还添加了一个示例以使标签说明转换是什么!:) 希望这会有所帮助。

#import
#main function
from Tkinter import *
def main():
    root=Tk()

    root.title("Temperature Converter")
    root.geometry("400x700")
    #someothersting=""
    someotherstring=""
#enter Celcius
    L1=Label(root,text="Enter a Celcius temperature.")
    E1=Entry(root,textvariable=someotherstring)
    somebutton=Button(root, text="Total", command=lambda: convert(E1.get()))

    somebutton.pack()
    E1.pack()
    L1.pack()
    root.mainloop()#main loop


#convert Celcius to Fahrenheit
def convert(somestring, label):
    if somestring != "":
        cel=int(somestring)
        far=(9/5*(cel))+32
        answer = str(cel) + " Converted to Farenheit = " + str(far)
        label.config(text=answer)

main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多