【发布时间】:2015-07-13 02:22:43
【问题描述】:
编写一个带有复选按钮的 GUI 程序,允许用户选择任何或所有这些服务。当用户单击按钮时,将显示总费用。
足够简单。这是我的代码:
import tkinter
import tkinter.messagebox
class auto:
def __init__(self):
#create main window
self.main_window=tkinter.Tk()
#create frames
self.top_frame=tkinter.Frame(self.main_window)
self.bottom_frame=tkinter.Frame(self.main_window)
#create value objects
self.oil=tkinter.IntVar()
self.lube=tkinter.IntVar()
self.rad=tkinter.IntVar()
self.trans=tkinter.IntVar()
self.inspect=tkinter.IntVar()
self.muff=tkinter.IntVar()
self.tire=tkinter.IntVar()
#set values
self.oil.set(26)
self.lube.set(18)
self.rad.set(30)
self.trans.set(80)
self.inspect.set(15)
self.muff.set(100)
self.tire.set(20)
#create checkbutton widgets
self.oilb=tkinter.Checkbutton(self.top_frame,\
text="Oil Change- $26.00" ,\
variable=self.oil)
self.lubeb=tkinter.Checkbutton(self.top_frame,\
text= "Lube Job- $18.00",\
variable=self.lube)
self.radb=tkinter.Checkbutton(self.top_frame,\
text= "Radiator Flush- $30.00" ,\
variable=self.rad)
self.transb=tkinter.Checkbutton(self.top_frame,\
text= "Transmission Flush- $80.00",\
variable=self.trans)
self.inspectb=tkinter.Checkbutton(self.top_frame,\
text= "Inspection- $15.00",\
variable=self.inspect)
self.muffb=tkinter.Checkbutton(self.top_frame,\
text= "Muffler Replacement- $100.00",\
variable=self.muff)
self.tireb=tkinter.Checkbutton(self.top_frame,\
text= "Tire Rotation- $20.00",\
variable=self.tire)
def display_charge():
total=0
for var in(self.oil,self.lube,self.rad,self.trans,self.inspect,self.muff,self.tire):
total+=var.get()
total_l.config(text="{}.00".format(total))
#pack the check buttons
self.oilb.pack()
self.lubeb.pack()
self.radb.pack()
self.transb.pack()
self.inspectb.pack()
self.muffb.pack()
self.tireb.pack()
#create charge and quit buttons
self.display_button=tkinter.Button(self.bottom_frame, \
text= "Display Charges", command=self.display_charge)
self.quit_button=tkinter.Button(self.bottom_frame,\
text="Quit", command=self.mainwindow.destory)
#pack the buttons
self.display_button.pack(side='left')
self.quit_button.pack(side='left')
#pack frames
self.top_frame.pack()
self.bottom_frame.pack()
#start main loop
tkinter.mainloop()
mygui=auto()
当我运行程序时,框架是空白的。我做错了什么? 我在获取总费用时也遇到了问题(我不知道如何)。 但是,如果我什至无法让框架正确显示,那么这种困境就无效了。
如何让内容显示在框架中,以及如何计算总费用?
【问题讨论】:
-
看起来与this question 非常相似。
标签: python tkinter frame helper