仍然在学习Python,必须创建一个用于温度转换的GUI,按钮不起作用,字段也不起作用

2024-10-04 11:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

该项目是一个基于GUI的程序,允许用户在华氏度和摄氏度之间转换值。启动时,华氏度字段应显示为“32.0”,摄氏度字段应在华氏度以下显示“0.0”。输入字段应该是一个标记为“>>>>;”的按钮,单击时应转换为摄氏度。Cels下面的按钮。字段应标记为“<;<;<;<;”以转换为华氏温度。下面的一行应该包含按钮的说明。我有计算,和小部件,大部分。但是,我不能让字段在启动时读取32.0和0.0,而不是0.0和0.0。按钮也不起作用。我只上了第9周的Python课程,但我只专注于这一部分。 这是代码和当前窗口。在

from tkinter import *
import tkinter.messagebox

class _TemperatureConversion(Frame):

def __init__(self):
     #Sets up the window and widget
     Frame.__init__(self)
     self.master.title("Temperature Conversion")
     self.master.rowconfigure(0, weight = 5)
     self.master.columnconfigure(0, weight = 5)
     self.master.geometry("300x300")
     self.master.resizable(0,0)
     self.grid(rowspan = 1, columnspan = 1)

    # Calculates the Fahrenheit to Celsius conversion
     font = tkinter.font.Font(family = "Arial Black", size = 15)
     self._fahrLabel = Label(self, font = font, text = " Fahrenheit ")
     self._fahrLabel.grid(row = 0, column = 0)
     self._fahrVar = DoubleVar()
     font = tkinter.font.Font(family = "Arial", size = 13)
     self._FahrValue = Entry(self, font = font, fg = "blue", justify =      "center", width = 13,
                            textvariable = self._fahrVar, text = "32.0",)
     self._FahrValue.grid(row = 1, column = 0)

     # Calculates the Celsius to Fahrenheit conversion
     font = tkinter.font.Font(family = "Arial Black", size = 15)
     self._celsLabel = Label(self, font = font, text = " Celsius ")
     self._celsLabel.grid(row = 0, column = 1)
     self._celsVar = DoubleVar()
     font = tkinter.font.Font(family = "Arial", size = 13)
     self._CelsValue = Entry(self, font = font, fg = "red", justify = "center", width = 13,
                                 textvariable = self._celsVar)
     self._CelsValue.grid(row = 1, column = 1)

     # The command buttons
     self._button = Button(self, font = font,
                           text = " >>>> ",
                           command = self._FahrValueN)
     font = tkinter.font.Font(family = "Arial", size = 15)
     self._button.grid(row = 2, column = 0, columnspan = 1)

     self._button = Button(self, font = font,
                           text = " <<<< ",
                           command = self._CelsValue)
     font = tkinter.font.Font(family = "Arial", size = 15)
     self._button.grid(row = 2, column = 1, columnspan = 1)

     #### From here down is not to be indented
     #### Just indented to keep it in the window

def _fahrenheitToCelsius(FahrValue):
    Fahr = FahrValue
    Cels = ((Fahr - 32) * 5) / 9
    print(Cels)

def _celsiusToFahrenheit(CelsValue):
     Cels = CelsValue
     Fahr = ((Cels * 9) / 5) + 32
     print(Fahr)

def main():
    _TemperatureConversion().mainloop()
    FahrValue = float(input("Enter a Fahrenheit value to convert: "))
    FConvert = fahrenheitToCelsius(FahrValue)
    CelsValue = float(input("Enter a Celsius value to convert: "))
    CConvert = celsiusToFahrenheit(CelsValue)

main()

Tags: toselfmastersizetkintercolumnfamilygrid
1条回答
网友
1楼 · 发布于 2024-10-04 11:35:38

您已经将textvariable分配给华氏度字段,所以只需设置DoubleVar

self._fahrVar.set(32.0)

之后,您需要重命名方法,因为不存在名为_FahrValueN_CelsValue的方法。如果要更新条目,只需更新变量:

^{pr2}$

在您的main()函数中,有另一个输入只在GUI关闭时运行,这很奇怪。你可能想删除它。另外,将函数调用包装成这样:

if __name__ == "__main__":
    main()

这样可以避免在导入模块时调用main函数。在

相关问题 更多 >