0

I am trying to bind a function to a button, so that it is only called when you click the button. I know it is possible to use lambda, but the problem is that I have to pass variables through the function, therefore I can't use lambda.

The code below attempts to use the binding method of the button, but this is from intuition and it's not working:

from tkinter import *

root = Tk()

variable = StringVar()

entry = Entry(root, textvariable=variable, width=10)

entry.pack()

def print_something(event, n):

     print(n) # I know it is supposed to be indented ;)

btn = Button(root, text='print something')

btn.bind('<Button-1>', print_something(variable))

btn.pack()

root.mainloop()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Momo 6aye3
  • 95
  • 1
  • 7
  • 2
    When you post questions, format your code to make your question easier to read. When editing the question, highlight your code and press ctrl+k to format the text as code. – F_SO_K Dec 17 '17 at 13:03
  • It might not be working because of the indentation! SHow us what code you actually have that doesn't work and we might be able to help – doctorlove Dec 17 '17 at 13:03
  • 1
    this questions are all over stackoverflow, please consider searching first then write a question if the question does not exist or different. – Abhishta Gatya Dec 17 '17 at 13:23
  • I think my question is very badly expressed. Sorry about that. I posted another question that is much more specific to my program (I tried the answers from other questions). If you can answer it then it would be very much appreciated, and thanks for your advice about ctrl+k. – Momo 6aye3 Dec 26 '17 at 10:55
  • @Bryan Oakley is it possible for you to make a room that I can ask specific questions about the GUI that you made in 2011 because I am using it as a framework for my project. I would very much appreciate it if you do so. (noting that I am currently also trying to comprehend the answers that you gave about it on other questions). – Momo 6aye3 Dec 30 '17 at 18:55

1 Answers1

0

If I understand your question, you are looking for a way to make use of lambda expressions. Below are the steps:

Set a value to variable by coding:variable.set(2)

Remove the event parameter from that function.

Get that value printed by using the function of your code as a callback for the button method:

With binding:

Modify your btn code as follows:

btn.bind('<Button-1>', lambda a: print_something(variable.get()))

Code:

from tkinter import *

root = Tk()
variable = StringVar()
variable.set(2)
entry = Entry(root, textvariable=variable, width=10)
entry.pack()
def print_something(n):
     print(n) 
btn = Button(root, text='print something')
btn.bind('<Button-1>', lambda a: print_something(variable.get()))
btn.pack()
root.mainloop()

Without binding:

Modify your btn code as follows:

btn = Button(root, text='print something', command=lambda: print_something(variable.get()))

Here is what I mean in action:

from tkinter import *

root = Tk()
variable = StringVar()
variable.set(2)
entry = Entry(root, textvariable=variable, width=10)
entry.pack()
def print_something(n):
     print(n) 
btn = Button(root, text='print something', command=lambda: print_something(variable.get()))
btn.pack()
root.mainloop()

Note that your code needs to be cleaned.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Hi. Thanks for your answer, but unfortunately it didn't help me because my question is very badly expressed. Apologies! I posted another question that is much more specific to my program. If you can answer it then thank you very much! – Momo 6aye3 Dec 26 '17 at 10:52