带复选框的Python GUI

2024-06-16 13:04:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在过去的一周里,我一直在努力解决这个问题,需要一些帮助。我试图编写一个GUI来计算所有选中复选框的总数。以下是我目前为止的代码:

import tkinter
import tkinter.messagebox

class Joes_auto:

    def __init__(self):

        # Create the main window.
        self.main_window = tkinter.Tk()

        # Create the frames.  One for the checkbuttons,
        # one for the total frame, and one for the buttons
        self.top_frame = tkinter.Frame(self.main_window)
        self.mid_frame = tkinter.Frame(self.main_window)
        self.bottom_frame = tkinter.Frame(self.main_window)

        #Create the IntVar objects to use with the checkbuttons
        self.oil_change = tkinter.IntVar()
        self.lube_job = tkinter.IntVar()
        self.radiator_flush = tkinter.IntVar()
        self.transmission_flush = tkinter.IntVar()
        self.inspection = tkinter.IntVar()
        self.muffler_replacement = tkinter.IntVar()
        self.tire_rotation = tkinter.IntVar()

        # Set the IntVar objects to 0
        self.oil_change.set(0)
        self.lube_job.set(0)
        self.radiator_flush.set(0)
        self.transmission_flush.set(0)
        self.inspection.set(0)
        self.muffler_replacement.set(0)
        self.tire_rotation.set(0)

        # Create the Checkbutton widgets in the top_frame
        self.oil_change = tkinter.Checkbutton(self.top_frame, text = 'Oil Change ($30)', \
                                                variable=self.oil_change)
        self.lube_job = tkinter.Checkbutton(self.top_frame, text = 'Lube Job ($20)', \
                                            variable=self.lube_job)
        self.radiator_flush = tkinter.Checkbutton(self.top_frame, text = 'Radiator Flush ($40)', \
                                          variable=self.radiator_flush)
        self.transmission_flush = tkinter.Checkbutton(self.top_frame, text = 'Transmission Flush ($100)', \
                                              variable=self.transmission_flush)
        self.inspection = tkinter.Checkbutton(self.top_frame, text = 'Inspection ($35)', \
                                      variable=self.inspection)
        self.muffler_replacement = tkinter.Checkbutton(self.top_frame, text = 'Muffler Replacement ($200)', \
                                               variable=self.muffler_replacement)
        self.tire_rotation = tkinter.Checkbutton(self.top_frame, text = 'Tire Rotation ($20)', \
                                         variable=self.tire_rotation)

        # Pack the Checkbuttons
        self.oil_change.pack()
        self.lube_job.pack()
        self.radiator_flush.pack()
        self.transmission_flush.pack()
        self.inspection.pack()
        self.muffler_replacement.pack()
        self.tire_rotation.pack()

        # Create a total and quit button
        self.total_button = tkinter.Button(self.bottom_frame, text = 'Calculate Total', \
                                           command = self.total)
        self.quit_button = tkinter.Button(self.bottom_frame, text = 'Quit', \
                                           command = self.main_window.destroy)

        # Pack the buttons
        self.total_button.pack(side = 'left')
        self.quit_button.pack(side = 'left')

        # Pack the frames
        self.top_frame.pack()
        self.mid_frame.pack()
        self.bottom_frame.pack()

        # Start the mainloop
        tkinter.mainloop()

    def total(self):

        self.total = 0

        if self.oil_change.get() == 1:
            self.total += 30
        if self.lube_job.get() == 1:
            self.total += 20
        if self.radiator_flush.get() == 1:
            self.total += 40
        if self.transmission_flush.get() == 1:
            self.total += 100
        if self.inspection.get() == 1:
            self.total += 35
        if self.muffler_replacement.get() == 1:
            self.total += 200
        if self.tire_rotation.get() == 1:
            self.total += 20

        tkinter.messagebox.showinfo("Your total is", self.total)

joes_auto = Joes_auto()

每次我运行程序时都会得到AttributeError:Checkbutton没有属性'get'。我希望程序计算所有检查的服务的总数,并显示总数。在


Tags: thetextselfgetiftkintertopvariable
2条回答

首先创建一组IntVar,并将它们保存为Joes_auto实例的属性

self.oil_change = tkinter.IntVar()

等等,但是你用

^{pr2}$

所以现在self.oil_change引用Checkbutton,而不是IntVar。这就是为什么会出现这样的错误:IntVar有一个.get方法,但是Checkbutton小部件没有

所以你需要给你的checkbutton取不同于它们相关联的IntVars的名字。或者不用给他们起永久性的名字,就这么做吧

b = tkinter.Checkbutton(self.top_frame, text = 'Oil Change ($30)',
                                        variable=self.oil_change)
b.pack()
b = tkinter.Checkbutton(self.top_frame, text = 'Lube Job ($20)',
                                    variable=self.lube_job)
b.pack()

等等(我去掉了那些反斜杠-你不需要它们,因为你要在括号里继续一行。这也适用于方括号和大括号中)。在


顺便说一句,你可以通过改变

import tkinter

import tkinter as tk

那你就可以了

b = tk.Checkbutton(self.top_frame, text = 'Oil Change ($30)', variable=self.oil_change)

等等

在创建Checkbutton实例时,使用相同的名称重写IntVar变量。在

self.oil_change = tkinter.Checkbutton(self.top_frame, text = 'Oil Change ($30)',
                                      variable=self.oil_change)

通过为Checkbutton实例选择其他名称来避免这种情况。实际上,没有checkbox实例引用的用例;您可以立即打包而不必保存到变量:

^{pr2}$

total方法中还有另一个属性重写问题:该方法正在重写self.total;方法将被int对象替换。因为该方法是绑定的,不会在外部使用;因此除非您在其他地方访问total方法,否则不会出现任何症状,但仍然无效。在

我建议将self.total = 0改为{},因为{}只在方法中使用。在

def total(self):
    total = 0

    if self.oil_change.get() == 1:
        total += 30
    if self.lube_job.get() == 1:
        total += 20
    if self.radiator_flush.get() == 1:
        total += 40
    if self.transmission_flush.get() == 1:
        total += 100
    if self.inspection.get() == 1:
        total += 35
    if self.muffler_replacement.get() == 1:
        total += 200
    if self.tire_rotation.get() == 1:
        total += 20

    tkinter.messagebox.showinfo("Your total is", total)

相关问题 更多 >