There is an image button, when clicked, the self.ANX (True / False) state and the image on the button itself change (for the True state, one, for False, another).
Which of these options is better suited for my task and what are the differences in their work?
The first option with self.button and self.methodom for the button:
def create_frames(self): sidebar_frame = tk.Frame(self) ... # Кнопка: ANX (on/off) self.ANX = False ANXon_button_image = tk.PhotoImage(file='./img/sidebar/ANX_on.png') ANXoff_button_image = tk.PhotoImage(file='./img/sidebar/ANX_off.png') self.ANX_button = tk.Button(sidebar_frame, image=ANXoff_button_image, bd=0, command=self.ANX_button_clicked) self.ANX_button.onimg = ANXon_button_image self.ANX_button.offimg = ANXoff_button_image self.ANX_button.place(width=137, height=31, x=17, y=13) def ANX_button_clicked(self): if self.ANX is False: self.ANX = True self.ANX_button['image'] = self.ANX_button.onimg else: self.ANX = False self.ANX_button['image'] = self.ANX_button.offimg The second option is with the button (without self) and the usual function inside the method:
def create_frames(self): sidebar_frame = tk.Frame(self) ... # Кнопка: ANX (on/off) self.ANX = False def ANX_button_clicked(): if self.ANX is False: self.ANX = True ANX_button['image'] = ANX_button.onimg else: self.ANX = False ANX_button['image'] = ANX_button.offimg ANXon_button_image = tk.PhotoImage(file='./img/sidebar/ANX_on.png') ANXoff_button_image = tk.PhotoImage(file='./img/sidebar/ANX_off.png') ANX_button = tk.Button(sidebar_frame, image=ANXoff_button_image, bd=0, command=ANX_button_clicked) ANX_button.onimg = ANXon_button_image ANX_button.offimg = ANXoff_button_image ANX_button.place(width=137, height=31, x=17, y=13) Tried to use tkinter.Checkbutton , but this method does not work as it should.
import,class, etc) so that the code can be immediately run? Because the code is working here, but the details have been omitted so that the excess does not have to be read. - Trajectory