lundi 14 décembre 2020

Using one check button to control label via two different buttons?

When you run this code, you will see that the check button is already checked (which is fine), and if you click either pepperoni or cheese buttons, you will see that it changes the label's text depending on which button you clicked. This is exactly what I want it to do. But I cannot get the buttons to "pause" or simply "stop" performing their initial actions when I uncheck the check button. Does anyone know how to fix this? I am thinking of experimenting with a select-all checkbutton.

import tkinter as tk

class GUI:
    def __init__(self,rootWindow):
        self.rootWindow=rootWindow
        self.label = tk.Label(rootWindow, text="Pepperoni Pizza")
        self.label.grid(row=0,column=0)

        self.button1=tk.Button(rootWindow,text="Pepperoni",command=self.pepperoni)
        self.button1.grid(row=0,column=1)
        self.button2=tk.Button(rootWindow,text="Cheese",command=self.cheese)
        self.button2.grid(row=0,column=2)

        self.buttonsEnabled = tk.IntVar()
        self.buttonsEnabled.set(1)

        self.check1 = tk.Checkbutton(rootWindow,text="Enabled",variable=self.buttonsEnabled,
                                      onvalue=1,offvalue=0)
        self.check1.grid(row=1, column=0)

    def cheese(self):
        self.label.config(text="Cheese Pizza")
        print("buttonsEnabled=",self.buttonsEnabled.get())
            

    def pepperoni(self):
        self.label.config(text="Pepperoni Pizza")
        print("buttonsEnabled=",self.buttonsEnabled.get())
            

def main():
    global label
    rootWindow = Tk()

    gui = GUI(rootWindow)
    rootWindow.mainloop()

main()



Aucun commentaire:

Enregistrer un commentaire