【问题标题】:Celsius to Fahrenheit Converter GUI program python摄氏到华氏转换器GUI程序python
【发布时间】:2018-12-16 00:16:24
【问题描述】:

编写一个将摄氏温度转换为华氏温度的 GUI 程序。用户应该能够输入摄氏温度,单击按钮,然后看到等效的华氏温度。使用以下公式进行转换:

F= (9/5)C+32

F 是华氏温度,C 是摄氏温度。

这是我所拥有的,但是当我运行它时没有任何反应:

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

    root.title("Some GUI")
    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=int(somestring)
        far=(9/5*(cel))+32
        print(far)

【问题讨论】:

  • 你不能指望别人完全给你写代码,在这里询问问题/错误
  • 你永远不会调用main函数。

标签: python tkinter


【解决方案1】:

主要问题是缺少main()

您应该在代码的最后添加main()。那就是说你真的不需要 main() 函数开始。

您试图在输入字段中将字符串分配为文本变量,但这不会做任何事情。如果你想使用textvariable 参数,那么你需要使用StringVar()IntVar() 之类的东西。我们这里不需要这样的东西。我们可以在convert 函数中简单地使用entry.get() 方法。

通过将此get() 方法移动到convert 函数,我们可以从您的按钮命令中删除lambda。只需command=convert

通过这些更改,您可以获得如下所示的简单内容。

from tkinter import *


def convert():
    x = entry.get()
    if x != "":    
        cel=int(x)
        far=(9/5*(cel))+32
        print(far)

root=Tk()
root.title("Some GUI")
root.geometry("400x700")

Button(root, text="Total", command=convert).pack()
entry = Entry(root)
entry.pack()
Label(root,text="Enter a Celcius temperature.").pack()

root.mainloop()

【讨论】:

    【解决方案2】:

    1。您可以使用 Python 的 GUI 工具包为程序创建 GUI。您可以使用 TKinter 作为开始。还必须包含一个输入框,您将在其中放置摄氏温度数据。

    2。创建 GUI 后,只需从该框中获取值,然后使用您的公式计算华氏当量。您可以使用this 教程了解如何从输入框中获取数据。

    例如:

     string_answer = entryBox.get()
     celsius = int(string_answer)
     fahrenheit = (9/5)celsius + 32
    

    3。将数据显示到您的 GUI 界面。

    【讨论】:

    • 您不能指望其他人为您编写代码,先生。您应该自己创建它,我们将尽力帮助您解决有关程序的任何问题:)
    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多