lundi 14 décembre 2020

Formatting checkbox to change label when either button has been clicked?

Not sure how to really go about this, basically, I want to be able to change the label from Good morning to Good night and vice versa when I click the buttons. By this, I mean, if the checkbox is clicked, I should be able to click Morning and see the label change to Good Morning, then click Night and see it change to Good Night and continue this process until the checkbox is unclicked. In which neither button will be able to change the label. Overall, the checkbox is like the on switch for the buttons, without it, none of the buttons can perform their actions. So far I can only get it stayed checked for so many clicks before it unchecks itself.

import tkinter as ttk
from tkinter import Tk

class GUI:
    def __init__(self,rootWindow):
        self.label = ttk.Label(rootWindow, text="Good morning")
        self.label.grid(row=0,column=0)

        self.button1=ttk.Button(rootWindow,text="Morning",command=self.morning)
        self.button1.grid(row=0,column=1)
        self.button2=ttk.Button(rootWindow,text="Night",command=self.night)
        self.button2.grid(row=0,column=2)

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

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

    def night(self):
        self.label.config(text="Good night!")
        print("buttonsEnabled=",self.buttonsEnabled.get())

    def morning(self):
        self.label.config(text="Good morning")
        self.buttonsEnabled.set(1-self.buttonsEnabled.get())

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

    gui = GUI(rootWindow)
    rootWindow.mainloop()

main()



Aucun commentaire:

Enregistrer un commentaire