Python Forum
Passing arguments into function, tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing arguments into function, tkinter
#2
Pass attributes into the __init__, attributes used in methods of the class assign as class attributes

import tkinter as tk
import raspberryGPIO

functionsPage = tk.Tk()
functionsPage.geometry('1024x600')


class Toggle(tk.Frame):
    def __init__(self, button_label, which_button, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.which_button = which_button

        self.btn = tk.Button(self, text=button_label, width=20,
                             height=5, bg="yellow", command=self.clicked)
        self.btn.grid(column=0, row=0, pady=10)

    def clicked(self):
        if self.btn['bg'] == "yellow":
            self.btn.configure(bg="red")
            GPIO.output(self.which_button, GPIO.LOW)
        else:
            self.btn.configure(bg="yellow")
            GPIO.output(self.which_button, GPIO.HIGH)


which_button1 = 'Asign this'
which_button2 = 'Asign this'

btn1 = Toggle('button_label1', which_button1, functionsPage)
btn1.grid()
btn2 = Toggle('button_label2', which_button2, functionsPage)
btn2.grid()

functionsPage.mainloop()
Reply


Messages In This Thread
RE: Passing arguments into function, tkinter - by Yoriz - Apr-18-2020, 01:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,464 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 4,265 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 5,224 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,817 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,353 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Passing information with a function Krisve94 3 2,332 Jun-30-2021, 07:51 PM
Last Post: deanhystad
  [Tkinter] Passing variable to function. KDog 2 2,283 May-25-2021, 09:15 PM
Last Post: KDog
  tkinter get function finndude 2 3,128 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,968 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,613 Nov-24-2020, 11:28 PM
Last Post: Dale22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020