Python Forum
Passing arguments into function, tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing arguments into function, tkinter
#1
Hey folks,
I've got a very limited understanding of how to pass information through a function. I have a tkinter page with multiple buttons. (Just FYI I copied code from various google searches to make this work so I'm not even sure what **kwargs is doing for my purpose but I don't care at the moment that is not my question)

In this format I am curious how to give each button it's own text. I attempted using the variable 'button_label' but not sure how to pass this information into the class.

I also have an argument called 'which_button' and 'btnGPIO' which pass the information of which GPIO pin to toggle, but again not sure how to pass this info through the clicked function.

I'm sure this is fairly basic stuff but I cannot find a solution to either yet... any help is appreciated!!

from tkinter import *
import raspberryGPIO

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

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

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

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

btn1 = Toggle(functionsPage)
btn1.grid()
btn2 = Toggle(functionsPage)
btn2.grid()

functionsPage.mainloop()
Reply


Messages In This Thread
Passing arguments into function, tkinter - by nanok66 - Apr-18-2020, 06:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 5,076 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  Tkinter won't run my simple function AthertonH 6 3,890 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,892 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,563 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,031 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Passing information with a function Krisve94 3 2,210 Jun-30-2021, 07:51 PM
Last Post: deanhystad
  [Tkinter] Passing variable to function. KDog 2 2,166 May-25-2021, 09:15 PM
Last Post: KDog
  tkinter get function finndude 2 2,965 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,457 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  function in new window (tkinter) Dale22 7 5,169 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