Python GUI程序 - 框架没有显示 - 使用StringVar()

时间:2017-04-22 18:16:50

标签: python python-3.x tkinter

avg_frame没有显示外,一切正常。这只是我第二次尝试使用Python的GUI程序,所以我不确定我忘记或错过了什么。这是我第一次使用StringVar()所以也许我搞砸了?

提前感谢您的帮助!

## Test score average
#

import tkinter


class TestAvg:
    def __init__(self):
## create main window
        self.main_window = tkinter.Tk()
## create frames
        self.test1_frame = tkinter.Frame(self.main_window)
        self.test2_frame = tkinter.Frame(self.main_window)
        self.test3_frame = tkinter.Frame(self.main_window)
        self.avg_frame = tkinter.Frame(self.main_window)
        self.button_frame = tkinter.Frame(self.main_window)

## create labels
        self.test1_label = tkinter.Label(self.test1_frame, \
                                         text="Enter the score for test 1:")
        self.test1_entry = tkinter.Entry(self.test1_frame, \
                                         width=20)
        self.test1_label.pack(side='left')
        self.test1_entry.pack(side='left')


##  labels
        self.test2_label = tkinter.Label(self.test2_frame, \
                                         text="Enter the score for test 2:")
        self.test2_entry = tkinter.Entry(self.test2_frame, \
                                         width=20)
        self.test2_label.pack(side='left')
        self.test2_entry.pack(side='left')



## labels
        self.test3_label = tkinter.Label(self.test3_frame, \
                                         text="Enter the score for test 3:")
        self.test3_entry = tkinter.Entry(self.test3_frame, \
                                         width=20)
        self.test3_label.pack(side='left')
        self.test3_entry.pack(side='left')



        self.result_label = tkinter.Label(self.avg_frame, text="Average")
        self.avg = tkinter.StringVar()
        self.avg_label = tkinter.Label(self.avg_frame, \
                                       textvariable=self.avg)
        self.result_label.pack(side='left')
        self.avg_label.pack(side='left')




## create buttons
        self.calc_button = tkinter.Button(self.button_frame, \
                                          text="Average", \
                                          command=self.calc_avg)
        self.quit_button = tkinter.Button(self.button_frame, \
                                          text="Quit", \
                                          command=self.main_window.destroy)
        self.calc_button.pack(side='left')
        self.quit_button.pack(side='left')


        self.test1_frame.pack()
        self.test2_frame.pack()
        self.test3_frame.pack()
        self.button_frame.pack()

    def calc_avg(self):
        ## Get test scores and store in variables
        self.test1 = float(self.test1_entry.get())
        self.test2 = float(self.test2_entry.get())
        self.test3 = float(self.test3_entry.get())

        ## Calculate the average of the 3 test scores
        self.test_score_avg = self.test1 + self.test2 + self.test3 / 3


        ## Display the avg_label
        self.avg.set(str(round(self.test_score_avg, 1)))

testavg = TestAvg()

1 个答案:

答案 0 :(得分:0)

你错过了一条线

...
    self.test1_frame.pack()
    self.test2_frame.pack()
    self.test3_frame.pack()
    self.avg_frame.pack()    # this line
    self.button_frame.pack()
...

此外,计算平均值的线在数学上是错误的。你错过了括号。:)

self.test_score_avg = (self.test1 + self.test2 + self.test3) / 3