I implemented the following code in python to manipulate an own property (here button.text) of the pressed button. When executing the code I get the following error: "AttributeError: 'Gui' object has no attribute 'button'". Important for my example is, that the button is part of the class and created in its init. Other working examples with a global defined button I found and got running.
#!/usr/bin/python3
# -*- coding: iso-8859-1 -*-
import tkinter
from tkinter import *
from tkinter import ttk
class Gui:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.frame.pack(fill='both', side='top', expand='True')
self.button = Button(master=self.frame,
text='connect',
height=20,
width=80,
padx=1,
pady=1,
command=self.connect_disconnct(),
compound=LEFT)
self.button.grid(row=0, column=0, padx=5, pady=5, sticky='ew')
mainloop()
def connect_disconnct(self):
if self.button.test == connect:
print("Button pressed -> connect")
self.button.text = "disconnect"
else:
print("Button pressed -> disconnect")
self.button.text = "connect"
if __name__ == '__main__':
form = Tk()
Gui(form)
form.mainloop()
How the own button element can be passed to the callback function so for example the text of the calling object can be changed in the callback function?