I'm trying to generalise a function in my script by sending in the Class' attribute through the function call to an outside function, but since I have to call the attribute self.attribute_name inside the class and object_name.attribute.name outside it, I either get an error that no self.attribute_nameexists outside the code or that no object_name.attribute.nameexists inside. The part of my code concerned is as follows(this is just a fragment of the full code with many parts omitted):
class My_Window:
self.get_info_box = Entry(root)
self.entry_button = Button(root, text="Choose", command =lambda: self.retrieve_input(self.get_info_box, solitaire.cards))
def retrieve_input(self, box, entity):
self.user_input = box.get()
entity = input_check(box)
def input_check(which_box): # Function outside class
my_window.which_box.delete(0, "end") # This is want I would like to do if it worked
return 0
my_window = My_Window()
Something in the back of my head tells me it might be possible to use lambda again to accomplish this but I'm still not sure how to use them properly and I couldn't find any active questions covering this specific case.
Anyone have any ideas how to work this out?
I think what you want is
def input_check(which_box):
getattr(my_window,which_box).delete(0, "end")
return 0
input_check("get_info_box")
but its hard to tell for sure
Try it without the my_window.
def input_check(which_box):
which_box.delete(0, "end")
return 0
Incidentally, entity = input_check(box) won't cause solitaire.cards to retain the value returned by input_check, because assignment doesn't propagate upwards like that. If you want to change solitaire.cards, you'll need to do solitaire.cards = input_check(box) instead. If solitaire isn't visible inside retrieve_input, then you'll need to make it an attribute of self.
Related
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 1 year ago.
I would like to understand how a button is working using lambda.
I have the following Python code:
from tkinter import *
def comando_click(mensagem):
print(mensagem)
menu_inicial = Tk()
menu_inicial.geometry("500x250+200+200")
botao = Button(menu_inicial, text = "Executar", command=comando_click("Nova_Mensagem"))
botao.pack()
menu_inicial.mainloop()
But my button doesn't work when I click on it, it only shows the print once in the console when I run the code, I added some prints here in the question:
Problem Picture one
Well it seems that when I use the Lambda function in the button it works and I really would like to know why.
Lambda working button Picture one
I just added to the button the lambda :
botao = Button(menu_inicial, text = "Executar", command=lambda:comando_click("Nova_Mensagem"))
Lambda working button Picture two
Why with lambda it works?
It shoudn't work without lambda too since lambda is basically a anonymous function?
I am extremely curious to understand why it works, thank you all for the help :)
Edit: I would like to thank you guys, now I finally understand what was going on and how Python was working. Thank you all very much :D
When you use () with a function name(func(args)), then it is immediately calling/invoking the function while python is executing the line, you do not want that. You want to ONLY call the function when the button is clicked. tkinter will internally call the function for you, all you have to do is give the function name.
Why use lambda? Think of it as a function that returns another function, your code can be lengthened to:
func = lambda: comando_click("Nova_Mensagem")
botao = Button(menu_inicial, text = "Executar", command=func)
func is the function name and if you want to call it, you would say func(). And when you say command=comando_click("Nova_Mensagem") then command has the value returned by command click(because you call the function with ()), which is None and if I'm not wrong, if the given value is None, it will not be called by tkinter. Hence your function is executed just once because of () and as a result of calling the function, you are assigning the value of the function call(None) before the event loop starts processing the events.
Some other methods:
Using partial from functools:
from functools import partial
botao = Button(.....,command=partial(comando_click,"Nova_Mensagem"))
Using a helper function:
def helper(args):
def comando_click():
print(args)
return comando_click
botao = Button(...., command=helper("Nova_Mensagem"))
IMO, lambdas are the easiest way to proceed with calling a function with arguments.
In this code:
command=comando_click("Nova_Mensagem")
you have called the comando_click function, once, and assigned the result (None) to the command argument. Nothing will happen when command is called (in fact you should get a TypeError exception because None is not callable).
In this code:
command=lambda:comando_click("Nova_Mensagem")
you have not actually called comando_click yet -- you have created a new function (using lambda) that will in turn call comando_click when it is called. Every time the button is clicked, your new function will get called.
If the lambda is confusing, you can do the exact same thing with a def like this:
def button_command():
comando_click("Nova_Mensagem")
...
command=button_command # no ()! we don't want to actually call it yet!
The lambda expression is just an alternative to using def when you want to create a small single-use function that doesn't need a name (e.g. you want to make a function that calls another function with a specific argument, exactly as you're doing here).
The issue is that with comando_click("Nova_Mensagem") you are executing the function. So command=None.
In the second case lambda:comando_click("Nova_Mensagem") is returning a lambda, that internally calls comando_click("Nova_Mensagem").
Fix: just put command=comando_click.
If you want to personalize the lambda with arguments you could write something like this:
def handler(args):
def custom_handler():
print(args)
return custom_handler
botao = Button(menu_inicial, text = "Executar", command=handler("my custom string"))
This is my first question on StackOverflow. So far lurking was enough to solve all my problems.
I'm a python newbie and I don't fully understand the meaning behind 'self' yet.
I defined a function (not a method. It's not inside a class) as
def pcal_thresh(self):
p_th = p_thresh.get()
print('p_th')
I am trying to use it in 2 separate conditions. First as a command for Tkinter
p_thresh = tk.Scale(calibration, from_=255, to=1, length=int(y_height*1.2), command=pcal_thresh)
Second, inside another function
def confirm():
if not top_distance == 0:
pcal_thresh()
In this exact configuration the function "pcal_thresh()" executes correctly as a Tkinter command, but not inside another function. If I remove 'self' from the declaration, it's the opposite. Works fine when used inside a function, but not as a Tkinter command. What can be the issue here?
self does not have a default value, so even if you don't use it, you still need to provide a value when you call pcal_thresh. As a callback, it receives the new scale value when called.
Either provide a dummy argument
def confirm():
if not top_distance == 0:
pcal_thresh(None)
or provide a default value:
def pcal_thresh(self=None):
p_th = p_thresh.get()
print('p_th')
I have managed to get a widget to appear by calling a function, then make it disappear by using the destroy method.
Unfortunately, the only way I managed to do that is by making the object global, which I understand is a bad way of doing things. I have tried several times to destroy the object without using global, but none of them worked.
Here's the relevant code:
def hayHijos():
print("ii")
z=var9.get()
if z==1:
var10 = StringVar()
global numHijosMat
numHijosMat=OptionMenu(app,var10,1,2,3,4,5,6,7,8,9,10)
numHijosMat.grid(column=2)
elif z==0:
print("aa")
numHijosMat.destroy()
var9 = IntVar()
hijosEntrePartes = Checkbutton(app, text="Hijos entre las partes", variable=var9, command=hayHijos)
hijosEntrePartes.var=var9
hijosEntrePartes.grid()
Two general possibilities.
Either you create a class context to keep track of elements such as widgets using class variables (self.widget for example). Therefor have a look at the class documentation
You return and pass the widget to / from your function.
This is not very suitable for callbacks but a general approach
def function(var, widget=None):
""" Widget is an optional argument. If not passed, it is initialized with None """
if var.get()==1:
# some code to create your widget
# make sure to use 'widget = ...' to create it
# to have the reference for your return
# call at the end of the function
else:
# some code to destroy your widget
# e.g. widget.destroy()
return widget
Using this code makes it easy for you to use the widget without making it global. Another variable is used in a global behaviour in your code. "var9". You should also pass this one on.
You would need to adapt your callback using maybe a lambda to pass both.
More recommended would be the class approach over here as lambdas often lack in scope of readability of code and reusability of code. (Could call it also "bad habit" but IMHO this is highly influenced by use case)
If you want to reuse the widget and only want to make it appear / disappear as the use of a Checkbutton suggests, I would rather recommend grid_remove method instead of widget.destroy method
relative noob here! I'm running 2.7, if that helps.
I'm trying to call a function defined in my main application class in a different function (I think that's called inheritance?) But I keep having problems with the number of args I put into my function!
Here's the function (is it called a method? if not, what's a method) I'm trying to call:
def student_list_updater(self, list):
self.StudentListDisplay.delete(0, END)
for student in list:
self.StudentListDisplay.insert(END, student)
And here's the function I'm calling it in (it's inheriting student_list_updater, right?):
def OnRemoveClick(self, student_list_updater):
self.student_list_updater = student_list_updater
index = self.StudentListDisplay.curselection()
index = int(index[0])
del student_list_temp[index]
self.student_list_updater(student_list_temp)
Thank you for the help in advance!
It's a little difficult to understand your question without more of the code, but hopefully this answer points you in the right direction.
First, to clarify, methods are just functions that can be accessed through an instance of a class, so yes, these are methods, but they're also functions--don't get too hung up on it. Next, I don't think inheritance is necessary here--inheritance will be one class inheriting attributes from another, and I believe all of your methods are only in one class (correct me if I'm mistaken).
Now, as to your code: it's giving you an error that one of your methods takes a number of arguments, and you gave it a different number. Well, to me, it looks like you only need to pass one argument for this whole process: student_list_temp to student_list_updater(). Once again, I can't say for sure that this will solve your problems, based on the lack of code you posted, but this might work:
def student_list_updater(self, studentlist): #change list to studentlist,
self.StudentListDisplay.delete(0, END) #Python already has a list() method
for student in studentlist:
self.StudentListDisplay.insert(END, student)
def OnRemoveClick(self): #Remove student_list_updater from the args, it has no value
#self.student_list_updater = student_list_updater #this doesn't do anything
index = self.StudentListDisplay.curselection() #This part I can't really comment on
index = int(index[0]) #without knowing the contents of the
del student_list_temp[index] #Listbox and student_list_temp,
self.student_list_updater(student_list_temp) #but this should call student_list_updater()
#and update the Listbox if it's working
The last thing I want to point out is how you call OnRemoveClick() will probably change. If you're calling it from a Button, it would look like this:
self.btn = Button(self, text='GO', command=self.OnRemoveClick)
Note that you're not passing any arguments to it.
Hope that helps. You also might want to take a look at https://docs.python.org/2/tutorial/classes.html and https://docs.python.org/2/tutorial/modules.html to clear up any classes and functions questions you might have.
I'm trying to create a Scrabble game with Python. I'd like to display the points that the word worth when the user is typing the word.
I already asked this question as I didn't know what method to use. As I discovered which method to use, and my question is about how to use this method, I think this deserve a new question.
My problem is that I created a function called bind_entry(event) that is supposed to set a label every time the user type a letter. But the function bind_entry(event)doesn't know the label to set and the entry where the word is.
Here is my code :
#this the function creating the label
def create_variabletext_intlabel(root,col,row):
val=IntVar()
label=Label(root,textvariable=val)
label.grid(column=col,row=row)
return val, label
#this is the function creating the entry
def create_entry_string(root,width,col,row,columnspan,rowspan):
val=StringVar()
entry=ttk.Entry(root,width=width,textvariable=val)
entry.grid(column=col,row=row,columnspan=columnspan,rowspan=rowspan)
entry.bind("<Any-KeyPress>",bind_entry)
#Here is my problem, when I call the function bind_entry.
return val, entry
def bind_entry(event):
label.set(m.counting_point(char(event)))
# m.counting_point() is a function counting the word's points
# my problem is that the function doesn't know yet the label.
# I don't know how to call the label.
# I call the function create_entry_string in another file initiating
# all the widget for the GUI
val_entry_word, entry_word =g.create_entry_string(root,15,1,1,1,1)
# I call the function create_variabletext_intlabel in another file
# initiating all the widget for the GUI
val_points,label_points=g.create_variabletext_intlabel(root,1,2)
I just noticed that the function m.counting_points() will count only the letter that is typed by the user. Here I should call val_entry_word.
So here is my question :
As val_entry_word and val_points are created in a function in another file How could I call val_entry_word and val_points in the function bind_entry() ?
Generally, when you need different function calls to share information without passing it explicitly, the best practice is to use a class.
e.g.
class LabelUpdater(object):
def create_variabletext_intlabel(self,root,col,row):
val=IntVar()
self.label=label=Label(root,textvariable=val)
label.grid(column=col,row=row)
return val, label
#this is the function creating the entry
def create_entry_string(self,root,width,col,row,columnspan,rowspan):
val=StringVar()
entry=ttk.Entry(root,width=width,textvariable=val)
entry.grid(column=col,row=row,columnspan=columnspan,rowspan=rowspan)
entry.bind("<Any-KeyPress>",self.bind_entry)
#Here is my problem, when I call the function bind_entry.
return val, entry
def bind_entry(self,event):
self.label.set(m.counting_point(char(event)))
#At this point, I don't understand your code anymore since I don't know what g
#is or how it's create_entry_string method calls your create_entry_string function...
#I'll assume that the module where g's class is defined imports this file...
#If that's how it works, then the following may be ok, although probably not because
#of circular imports...
container=LabelUpdater()
create_variabletext_intlabel=container.create_variabletext_intlabel
create_entry_string=container.create_entry_string
val_entry_word, entry_word =g.create_entry_string(root,15,1,1,1,1) #somehow calls create_variabletext_intlabel which is mapped to container.create_variable_intlabel???
# I call the function create_variabletext_intlabel in another file
# initiating all the widget for the GUI
val_points,label_points=g.create_variabletext_intlabel(root,1,2)
Of course, you could also use globals...(though that is definitely discouraged)
Finally, an idiom that I often use to add additional information in a bind callback is to wrap the callback function in another function...
def myfunc(root):
label=Label(root,text="cow")
label.pack()
return label
#This is the callback we want...
# Q: but how do we pass S?
# A: we need to wrap this call in another -- a perfect use for lambda functions!
def change_label(label,S):
label.config(text=S)
root=Tk()
lbl=myfunc(root)
lbl.bind("<Enter>",lambda e: change_label("Horse"))
lbl.bind("<Leave>",lambda e: change_label("Cow"))