Python Tkinter框架毛坯
编写一个带有复选按钮的GUI程序,允许用户选择任何或所有这些服务。当用户单击按钮时,将显示总费用 很简单。这是我的密码:Python Tkinter框架毛坯,python,tkinter,frame,helpers,totals,Python,Tkinter,Frame,Helpers,Totals,编写一个带有复选按钮的GUI程序,允许用户选择任何或所有这些服务。当用户单击按钮时,将显示总费用 很简单。这是我的密码: import tkinter import tkinter.messagebox class auto: def __init__(self): #create main window self.main_window=tkinter.Tk() #create frames self.top_fr
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()
当我运行程序时,帧是空白的。我做错了什么?
我在获取总费用时也遇到了问题(我不知道如何获取)。
但是,如果我连画框都不能恰当地显示出来,那么这种困境是无效的
如何在画面中显示内容,以及如何计算总费用?您的代码有几个问题
tkinter
app的非标准类结构,包括初始化和mainloop()
main window
/main\u window
和destroy
/destory
display\u charge()
所做的远不止是显示费用;它还完成了一半的GUI设置display\u charge()
引用了一个不存在的total\l
对象Checkbutton
s不是那样工作的。清除它们时,它们被设置为0,检查它们时,它们被设置为1。如果使用自定义值设置()它们,它们将是正确的,直到您切换它们,此时它们将恢复为默认值。要正确地为它们指定自定义值,请使用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()