编写带有检查按钮的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()
运行程序时,框架为空白。我做错了什么? 我在收取总费用时遇到了问题(我不知道怎么做)。 但如果我不能让框架正确显示,那么这种困境是无效的。
如何在框架中显示内容,以及如何计算总费用?
答案 0 :(得分:2)
您的代码有几个问题。
tkinter
应用的非标准类结构,包括初始化和mainloop()
。mainwindow
/ main_window
和destroy
/ destory
。display_charge()
做的不仅仅是显示费用;它也是GUI设置的一半。display_charge()
引用了一个不存在的total_l
对象。Checkbutton
不这样做。当你清除它们时,它们被设置为0,当你检查它们时,它们被设置为1.如果你set()
它们具有自定义值,它们将是正确的,直到你切换它们,此时它们将恢复到他们的默认值。要正确地为其提供自定义值,请使用offvalue
和onvalue
属性。\
)。import tkinter
class auto:
def __init__(self, parent):
# create reference to main window
self.main_window = parent
#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()
#create checkbutton widgets
self.oilb=tkinter.Checkbutton(self.top_frame,
text="Oil Change- $26.00" ,
variable=self.oil, onvalue=26)
self.lubeb=tkinter.Checkbutton(self.top_frame,
text= "Lube Job- $18.00",
variable=self.lube, onvalue=18)
self.radb=tkinter.Checkbutton(self.top_frame,
text= "Radiator Flush- $30.00" ,
variable=self.rad, onvalue=30)
self.transb=tkinter.Checkbutton(self.top_frame,
text= "Transmission Flush- $80.00",
variable=self.trans, onvalue=80)
self.inspectb=tkinter.Checkbutton(self.top_frame,
text= "Inspection- $15.00",
variable=self.inspect, onvalue=15)
self.muffb=tkinter.Checkbutton(self.top_frame,
text= "Muffler Replacement- $100.00",
variable=self.muff, onvalue=100)
self.tireb=tkinter.Checkbutton(self.top_frame,
text= "Tire Rotation- $20.00",
variable=self.tire, onvalue=20)
#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.main_window.destroy)
self.total_l = tkinter.Label(self.bottom_frame, text="$0.00")
#pack frames
self.top_frame.pack()
self.bottom_frame.pack()
#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()
#pack the buttons
self.display_button.pack(side='left')
self.quit_button.pack(side='left')
self.total_l.pack(side='left')
def display_charge(self):
self.total_l.config(text="${}.00".format(sum(map(tkinter.IntVar.get,
[self.oil, self.lube, self.rad, self.trans, self.inspect, self.muff,
self.tire]))))
root=tkinter.Tk()
mygui=auto(root)
root.mainloop()