Python Forum
[Tkinter] How to add an argument to a buttons command call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to add an argument to a buttons command call
#1
Hi!

using Python 3.6 and Tkinter

Im calling a method with a button. The method will change text on a label

Here is my button:

self.button5 = Button(middleframe6, text="Configure machine", command=self.ReceiptFormat, relief=RAISED)
which calls the method:
def ReceiptFormat(self, *args):
    self.labelwindow['text'] = "Please select receipt Logo"
The label will get the new text
self.labelwindow = Label(master)
        self.labelwindow.place(relx=0.45, rely=0.4, relwidth=0.47, relheight=0.47)
This works fine, but the problem is when im passing in an argument from the command. So im doing this:

self.button5 = Button(middleframe6, text="Configure RVM", command=self.ReceiptFormat("text"), relief=RAISED
The methode im calling
def ReceiptFormat(self, confirm, *args):
        newtext = confirm
        self.labelwindow['text'] = newtext
When im calling the method with the argument i get the following error message "AttributeError: 'SendT70' object has no attribute 'labelwindow'"

Any idead why i cant change the label text when im using arguments when calling the method?
Reply
#2
To add arguments to a function called as a command you can use partial from functools
from functools import partial

	
self.button5 = Button(middleframe6, text="Configure RVM", command=partial(self.ReceiptFormat, "text"), relief=RAISED
Reply
#3
That works, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 5,691 Nov-11-2021, 08:45 PM
Last Post: menator01
  Syntax Error: Positional argument follows keyword argument Rama02 3 4,025 Feb-09-2021, 06:10 PM
Last Post: deanhystad
  Creating a frame with 4 command buttons Heyjoe 5 2,404 Aug-21-2020, 03:16 PM
Last Post: deanhystad
  [Tkinter] Command button, then more command buttons Heyjoe 4 2,820 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] Creation of Buttons with Shared Command Inside Class MulliganAgain 1 1,683 Jul-08-2020, 06:22 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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