Tkinter GUI窗口空白

时间:2016-01-30 23:25:04

标签: python tkinter

我决定尝试编写一个程序,根据身体脂肪测试仪的测量值来计算你的体脂百分比。几年前我为一个好友创建了一个excel电子表格,这样他就可以计算出他的客户体脂百分比。我认为创建一个让他使用的程序会很酷,而且它也会给我一些练习。

当我运行程序时,它似乎运行没有任何问题,除了Tkinter窗口完全空白的事实。任何人都可以在我的代码中识别我做错了吗?

此外,如果我得到这个工作,无论如何我是否可以创建一个可执行文件给我的伙伴,或某种方式让桌面运行脚本来运行python代码?他会使用Mac,所以我已经安装了python,我觉得可能有一些方法可以运行终端脚本,但我不知道怎么做。

非常感谢任何帮助,代码如下:

__author__ = 'jared'

import tkinter
import tkinter.messagebox
import cmath

class BodyFatGUI:

 def __init__(self):

    #Create Main Window
    self.main_window = tkinter.Tk()

    #Create Frames for wigets
    self.gender_frame = tkinter.Frame(self.main_window)
    self.tricep_frame = tkinter.Frame(self.main_window)
    self.chest_frame = tkinter.Frame(self.main_window)
    self.abdominal_frame = tkinter.Frame(self.main_window)
    self.suprailiac_frame = tkinter.Frame(self.main_window)
    self.thigh_frame = tkinter.Frame(self.main_window)
    self.button_frame = tkinter.Frame(self.main_window)

    #Initialize Radio Buttons
    self.radio_var = tkinter.IntVar()
    self.radio_var.set(1)

    #Create gender widgets
    self.rb1 = tkinter.Radiobutton(self.gender_frame, text = 'Male', variable=self.radio_var, \
                               value = 1)

    self.rb2 = tkinter.Radiobutton(self.gender_frame, text = 'Female', variable=self.radio_var, \
                               value = 2)

    #Create age widget
    self.age_label = tkinter.Label(self.gender_frame, text='Age:')
    self.age_entry = tkinter.Entry(self.gender_frame, width='7')

    #Create skinfold widgets
    self.tricep_label = tkinter.Label(self.tricep_frame, text='Enter Tricep skinfold measurement:')
    self.tricep_entry = tkinter.Entry(self.tricep_frame, width=10)
    self.chest_label = tkinter.Label(self.chest_frame, text='Enter Chest skinfold measurement:')
    self.chest_entry = tkinter.Entry(self.chest_frame, width=10)
    self.abdominal_label = tkinter.Label(self.abdominal_frame, text = 'Enter Abdominal measurement:')
    self.abdominal_entry = tkinter.Entry(self.abdominal_frame, width=10)
    self.suprailiac_label = tkinter.Label(self.suprailiac_frame, text='Enter Suprailiac measurement:')
    self.suprailiac_entry = tkinter.Entry(self.suprailiac_frame, width=10)
    self.thigh_label = tkinter.Label(self.thigh_frame, text='Enter Thigh measurement:')
    self.thigh_entry = tkinter.Entry(self.thigh_frame, width=10)

    #create button frame
    self.calc_button = tkinter.Button(self.button_frame, text='Calculate', command=self.bodyfat)
    self.quit_button = tkinter.Button(self.button_frame, text='Quit', command=self.main_window.destroy)

    #Pack the widgets
    self.rb1.pack(side='left')
    self.rb2.pack(side='left')
    self.age_label.pack(side='right')
    self.age_label.pack(side='right')
    self.tricep_label.pack(side='left')
    self.tricep_entry.pack(side='left')
    self.chest_label.pack(side='left')
    self.chest_entry.pack(side='left')
    self.abdominal_label.pack(side='left')
    self.abdominal_entry.pack(side='left')
    self.suprailiac_label.pack(side='left')
    self.suprailiac_entry.pack(side='left')
    self.thigh_label.pack(side='left')
    self.thigh_entry.pack(side='left')
    self.calc_button.pack(side='left')
    self.quit_button.pack(side='left')

    tkinter.mainloop()

 def bodyfat(self):
    #Get value entered from user

    tri = float(self.tricep_entry.get())
    chest = float(self.chest_entry.get())
    ab = float(self.abdominal_entry.get())
    sup = float(self.suprailiac_entry.get())
    thigh = float(self.thigh_entry.get())
    age = float(self.age_entry.get())

    sum = tri + chest + ab + sup + thigh

    if self.radio_var.get() is 'Male':

        bd = 1.1093800 - 0.008267*sum + 0.0000016*sum^2 - 0.0001392*age
        fat = ((4.570/bd) - 4.142)*100
        tkinter.messagebox.showinfo('Results', 'Your calculated body fat percentage is: ' + str(fat))

    elif self.radio_var.get() is 'Female':

        bd = 1.0994921 - 0.0009929*sum + 0.0000023*sum^2 - 0.0001392*age
        fat = ((4.570/bd) - 4.142)*100
        tkinter.messagebox.showinfo('Results', 'Your calculated body fat percentage is: ' + str(fat))

body_fat = BodyFatGUI()

1 个答案:

答案 0 :(得分:1)

您忘了对所有帧使用pack()

你需要把它放在窗口中:

self.gender_frame.pack()
self.tricep_frame.pack()
self.chest_frame.pack()
self.abdominal_frame.pack()
self.suprailiac_frame.pack()
self.thigh_frame.pack()
self.button_frame.pack()