【问题标题】:Tkinter frame blankTkinter 框架空白
【发布时间】: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


【解决方案1】:

您的代码有几个问题。

  1. tkinter 应用的非标准类结构,包括初始化和mainloop()
  2. 错别字,如@9​​87654323@/main_windowdestroy/destory
  3. display_charge() 所做的不仅仅是显示费用;它也完成了一半的 GUI 设置。
  4. display_charge() 引用了一个不存在的 total_l 对象。
  5. Checkbuttons 不是这样工作的。当您清除它们时,它们被设置为 0,当您检查它们时,它们被设置为 1。如果您使用自定义值 set() 它们,它们将是正确的,直到您切换它们,此时它们将恢复为它们的默认值。要正确地为它们提供自定义值,请使用 offvalueonvalue 属性。
  6. 这是样式问题而不是功能问题,但除非必要,否则尽量不要使用显式续行 (\)。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多