简体   繁体   中英

How to change multiple buttons text using only one function, tkinter python

I would like to use only one function (self.toggle) for all of my buttons in my GUI instead of doing each for every button. I have tried to change name of self.button only without numbers and then I had text changing on just one button not the one required. Is there simple way to do just one function? I'm just starting with Python. Thanks in advance.

from tkinter import *
from tkinter.ttk import Button

class Application(Tk):

def __init__(self, *args, **kwargs):
    Tk.__init__(self, *args, **kwargs)
    self.create_widgets()
    self.number = [1, 2, 3]
    self.mainloop()

def toggle1(self, number):

        if self.button1.config('text')[-1] == f"{number}: ON":
            self.button1.config(text=f"{number}: OFF")
            print("OFF")
        else:
            self.button1.config(text=f"{number}: ON")
            print("ON")

def toggle2(self, number):

        if self.button2.config('text')[-1] == f"{number}: ON":
            self.button2.config(text=f"{number}: OFF")
            print("OFF")
        else:
            self.button2.config(text=f"{number}: ON")
            print("ON")

def toggle3(self, number):

        if self.button3.config('text')[-1] == f"{number}: ON":
            self.button3.config(text=f"{number}: OFF")
            print("OFF")
        else:
            self.button3.config(text=f"{number}: ON")
            print("ON")

def create_widgets(self):
    self.title("application")

    width = 200
    height = 250
    screen_width = self.winfo_screenwidth()
    screen_height = self.winfo_screenheight()
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    self.geometry("%dx%d+%d+%d" % (width, height, x, y))

    self.button1 = Button(self, text = "1: OFF", command=lambda: self.toggle1(1))
    self.button1.grid(
        row=3, column=0, sticky="w", columnspan=1, padx=0, pady=10)

    self.button2 = Button(self, text = "2: OFF", command=lambda: self.toggle2(2))
    self.button2.grid(
        row=5, column=0, sticky="w", columnspan=1, padx=0, pady=10)

    self.button3 = Button(self, text = "3: OFF", command=lambda: self.toggle3(3))
    self.button3.grid(
        row=7, column=0, sticky="w", columnspan=1, padx=0, pady=10)


if __name__ == "__main__":
    Application()

You can pass the button widget to toggle() as below:

...
class Application(Tk):
    ...
    def toggle(self, button):
        number, state = button['text'].split(': ')
        state = 'ON' if state == 'OFF' else 'OFF'
        button['text'] = f'{number}: {state}'
        print(number, state)

    def create_widgets(self):
        ...
        self.button1 = Button(self, text="1: OFF", command=lambda: self.toggle(self.button1))
        ...
        self.button2 = Button(self, text="2: OFF", command=lambda: self.toggle(self.button2))
        ...
        self.button3 = Button(self, text="3: OFF", command=lambda: self.toggle(self.button3))
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM