为什么我的Python仅显示一个空的tkinter框?

时间:2018-07-08 06:02:03

标签: python user-interface tkinter

我正在为我的软件开发入门课程做作业。分配的目的是创建一个GUI,该GUI允许用户根据所按下的按钮将通用温度转换为摄氏温度或华氏温度。我编写的代码似乎可以正常运行(我使用的是IDLE),但是当我运行它时,它只显示一个空的tk框。有什么我忘了吗?

import tkinter

class tempConverter:

    def __init__(self):

        # create window
        self.mainWindow = tkinter.Tk()

        # create frames
        self.topFrame = tkinter.Frame()
        self.midFrame = tkinter.Frame()
        self.botFrame = tkinter.Frame()

        # create input label
        self.inputLabel = tkinter.Label(self.topFrame, text='Enter a 
        temperature: ')
        self.tempEntry = tkinter.Entry(self.topFrame, width=10)

        # pack input label
        self.inputLabel.pack(side='left')
        self.tempEntry.pack(side='left')

        # create info labels
        self.infoFarLabel = tkinter.Label(self.midFrame, text='Converted 
        to Fahrenheit: ')
        self.infoCelLabel = tkinter.Label(self.midFrame, text='Converted 
        to Celsius: ')

        # create StringVar to hold output
        self.Celsius = tkinter.StringVar()
        self.Fahrenheit = tkinter.StringVar()

        # connect StringVar to Label
        self.celLabel = tkinter.Label(self.midFrame, 
        textvariable=self.Celsius)
        self.farLabel = tkinter.Label(self.midFrame, 
        textvariable=self.Fahrenheit)

        # pack those labels
        self.infoFarLabel.pack(side='left')
        self.farLabel.pack(side='left')
        self.infoCelLabel.pack(side='right')
        self.celLabel.pack(side='right')

        # create conversion buttons
        self.fConvertButton = tkinter.Button(self.botFrame, text='Convert 
        to Celsius', command=self.fConvert)
        self.cConvertButton = tkinter.Button(self.botFrame, text='Convert 
        to Fahrenheit', command=self.cConvert)
        self.quitButton = tkinter.Button(self.botFrame, text='Quit', 
        command=self.mainWindow.destroy)

        # pack buttons
        self.fConvertButton.pack(side='left')
        self.cConvertButton.pack(side='left')
        self.quitButton.pack(side='left')

        tkinter.mainloop()

    # define conversion methods
    def fConvert(self):
        temp = float(self.tempEntry.get())
        celsius = float((temp - 32)* (5 / 9))
        self.Celsius.set(celsius)

    def cConvert(self):
        temp = float(self.tempEntry.get())
        fahrenheit = float((temp * (9 / 5)) + 32)
        self.Fahrenheit.set(fahrenheit)

# create instance
tempConv = tempConverter()

0 个答案:

没有答案