I'm new to python programming and I need assistance in calling a function inside another function. My first function is:
def on_enter_save(event):
btnSave['background'] = 'gray'
winsound.PlaySound ('Hover.wav', winsound.SND_ASYNC)
def on_leave_save(event):
btnSave['background'] = '#25219c'
My second function is :
def New():
btnSave = tk.Button(main, text="Save", bg="#25219c", fg="white", width=5, height=1, relief="flat", command=save)
btnSave['font'] = MyFont2
btnSave.place(x=750, y=1000, anchor=CENTER)
btnSave.bind("<Enter>", on_enter_save)
btnSave.bind("<Leave>", on_leave_save)
I don't know what is the problem if anyone has a solution?
You need to make sure of 2 main things:
Create the function that you want to call before the function you call it in.
Don't forget to call the main function too.
This format works:
def calledFunction():
print("This function is called in another function!")
def function():
print("This is the first function!")
calledFunction()
function()
The only function call is the winsound.Playsound function, the remainder is redundant. As commented elsewhere, you need to provide more information. However, to answer the question as stated, this is how you do it:
def f1(msg):
print(msg)
print("Calling f2")
f2("Hello from f1")
def f2(msg):
print("Starting F2")
print(msg)
def main():
f1("Hello from main")
if __name__ == "__main__":
main()
Thank you all for your support. This solved the issue.
Related
I'm trying to run some python code on a raspberry-pi in order to simulate different sensors. When calling a function twice, I'm being told that I'm providing too many arguments. I will have approximately 20 different functions all calling this outputPWM function, but I have just shown two functions as an example.
I've only started using python this week, so I'm not entirely sure how to fix this. If I can clear the outputPWM function of arguments in some way before the next call or I may have some fundamental flaw in my code
def outputPWM(n1,i):
num1 = (n1.get())
result = int(num1)
dutycycle = result
print(List[25])
List[i].start(0)
List[i].ChangeDutyCycle(dutycycle)
print("Duty cycle is %d" % dutycycle)
print("output is on port %d" %i)
def control():
ControlReturn = tk.StringVar()
ControlLabel = tk.Label(master, text="Control Return Air Temperature (Degrees Celcius)").grid(row=0, column=0)
ControlResult = tk.Label(master)
ControlEntry = tk.Entry(master, textvariable=ControlReturn).grid(row=0,column=2)
global outputPWM
outputPWM=partial(outputPWM,ControlReturn,20)
buttonCal = tk.Button(master, text="Enter", command=outputPWM).grid(row=0, column=3)
def display():
DisplayReturn = tk.StringVar()
DisplayLabel = tk.Label(master,text="Display Return Air Temperature (Degrees Celcius)").grid(row=1, column=0)
DisplayEntry = tk.Entry(master, textvariable=DisplayReturn).grid(row=1,column=2)
global outputPWM
outputPWM= partial(outputPWM,DisplayReturn,25)
buttonCal = tk.Button(master, text="Enter", command=outputPWM).grid(row=1, column=3)
display()
control()
The error I receive is :
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1550, in __call__
return self.func(*args)
TypeError: outputPWM() takes exactly 2 arguments (4 given)
Wherever you are creating a partial function, give it a name other than the global outputPWM:
global outputPWM
outputPWM_ = partial(outputPWM, ControlReturn, 20)
buttonCal = tk.Button(master, text="Enter", command=outputPWM_).grid(row=0, column=3)
This code is executed twice, first time in display() and second time in control() (with slightly different parameters, but that does not matter)
global outputPWM
outputPWM=partial(outputPWM,ControlReturn,20)
It replaces the original function with derived partial one which adds 2 args. The second round replaces that partial function with another partial one derived from the first partial function. The resulting function adds 2+2 args in two steps. That is the source of "takes 2 args (4 given)" error.
The solution is not reusing the same name (see https://en.wikipedia.org/wiki/Variable_shadowing) and not using global unless it is not really necessary.
In the little GUI app below. When I use button's command option to call a function. It doesn't work like this: self.update() rather it works like this: self.update. Why so? Is is some special way that command option of a button works? I think a method or a function should be called with those braces (), unless it's a property:
i.e.
#name.setter:
def setter(self, name):
self.name = name
#main
object.name = "New_obj"
Note: The above is just a template so you might get my point. I didn't write the complete valid code. Including class and everything.
from tkinter import *
class MuchMore(Frame):
def __init__(self, master):
super(MuchMore,self).__init__(master)
self.count =0
self.grid()
self.widgets()
def widgets(self):
self.bttn1 = Button(self, text = "OK")
self.bttn1.configure(text = "Total clicks: 0")
self.bttn1["command"] = self.update # This is what I am taking about
self.bttn1.grid()
def update(self):
self.count += 1
self.bttn1["text"] = "Total clicks" + str(self.count)
#main
root = Tk()
root.title("Much More")
root.geometry("324x454")
app = MuchMore(root)
It is a high order function, meaning you are referencing a function as an object. You are not calling the function and assigning the command to the return value of the function. See here for more information.
The command parameter takes a reference to a function -- ie: the name of the function. If you add parenthesis, you're asking python to execute the function and give the result of the function to the command parameter.
I'm trying to create a couple of functions which do things in a sequential order. First they need to open a new window and display a label, then they need to wait for some seconds, then they need to call another function. However, I'm struggling to get the functions to wait, all the methods I've tried (.after, .sleep, .wait_visibility) seem to be ignored and it just skips to the next function call without pausing.
Here's what I have (sorry if it's messy, I'm new to python):
from tkinter import *
import time
root =Tk()
root.geometry('600x600')
def scale_screen(event = None):
global s_screen
s_screen = Toplevel(root)
s_screen.title('Residual Inhibition Tester')
s_screen.geometry('600x600')
s_screen.transient(root)
s_screen.bind('<Return>', sel)
global var
var = IntVar()
scale = Scale(s_screen, variable = var, orient = HORIZONTAL, length = 1000)
scale.focus_set()
scale.pack(anchor=CENTER)
button = Button(s_screen, text="Select", command=sel)
button.pack(anchor=CENTER)
def sel(event = None):
label = Label(s_screen)
selection = "Value = " + str(var.get())
label.config(text = selection)
interval_screen()
def interval_screen():
global i_screen
i_screen = Toplevel(root)
i_screen.geometry('600x600')
i_screen.transient(root)
i_label = Label(i_screen, text = "Please Wait")
i_label.pack(anchor = CENTER)
s_screen.destroy()
i_screen.after(3000, masker_screen)
#time.sleep(3)
#i_screen.after(300,i_label.configure(text="Playing New Masker Noise"))
#root.wait_visibility(window = i_screen)
def masker_screen():
global m_screen
m_screen = Toplevel(root)
m_screen.geometry('600x600')
m_screen.transient(root)
m_label = Label(m_screen, text = "Playing New Masker Noise").pack(anchor = CENTER)
m_screen.after(3000, lambda: scale_screen(event = None))
i_screen.destroy()
b1 = Button(root, command = scale_screen).pack(anchor=CENTER)
root.bind('<Return>', scale_screen)
root.mainloop()
In this example, the program will run but just skip the interval_screen entirely and just do the masker_screen. I'm also not averse to just using one screen and using the .configure methods to change the label text if that's easier.
Thanks!
Without seeing all the ways you tried it, it's impossible to know what you did wrong. In general you should never call time.sleep and you should never call after with just a single argument. Also, when you use after with two arguments, the second argument must be a reference to a function.
The proper way to do this is to have your first function call your second function via after:
def interval_screen():
...
i_screen.after(3000, maker_screen)
def masker_screen():
...
m_screen.after(3000, lambda: scale_screen(event = None))
Note that in your updated question you're using after incorrectly:
m_screen.after(3000, scale_screen(event = None))
You're calling the function scale_screen(...) immediately, and giving the result of that to the after function. If you need to pass arguments to your function you must create another function that does not require arguments. The simplest way to do this is with lambda, though you can also use functools.partial or you can create your own function.
from tkinter import *
def my_function(parameter1, parameter2):
total = int(entrada1.get()) + int(entrada2.get())
Label(root,text=calculated_property).pack()
root = Tk()
frame = Frame(root)
frame.pack()
root.title("Testing 123")
numero1 = IntVar()
numero2 = IntVar()
entrada1 = Entry(root,textvariable=numero1)
entrada1.pack()
entrada2 = Entry(root,textvariable=numero2)
entrada2.pack()
aceptar = Button(root,text="Calcular",command=my_function)
aceptar.pack()
root.mainloop()
I'm working on simple graphic interfaces in Python. I`m using the tkinter library to practice with.
The form generated is quite simple, it just consist of two inputs and a button that calls the function: my_function.
I have troubles calling that function because the attribute "command" does not allows any parameter, but my_function requires two that are given by the inputs of the form.
Then, the idea is calling several functions inside my_function and return in a label a calculated_property.
Can you give me any solution?
Thank U so much!
You can simply use a lambda function to pass the arguments:
aceptar = Button(root,text="Calcular", command=lambda: my_function(arg1, arg2))
This code is equivalent to doing:
def func():
my_function(arg1, arg2)
aceptar = Button(root,text="Calcular", command=func)
except that the function is created inline.
hey back again with the same code, well edited so it works better. anyway trying to add the button input into the array and that works. what doesn't work is the fact every time i call the function do() the values reset due to them being local. i tried to fix this by making it global(within the class) using the self.store array. this didn't seem to fix the problem so if someone could help would be much appreciated here is the relevant code
def __init__(self,master):#is the master for the button widgets
self.count=0
self.store=["0"]
frame=Frame(master)
frame.pack()
self.addition = Button(frame, text="+", command=self.add)#when clicked sends a call back for a +
self.addition.pack()
self.subtraction = Button(frame, text="-", command=self.sub)#when clicked sends a call back for a -
self.subtraction.pack()
self.equate = Button(frame, text="=", command=self.equate)#when clicked sends a call back for a =
self.equate.pack()
self.one = Button(frame, text="1", command=self.one)#when clicked sends a call back for a -
self.one.pack()
def add(self):
self.do("+")
self.count=self.count+1
def sub(self):
self.do("-")
self.count=self.count+1
def equate(self):
self.do("=")
def one(self):
self.do("1")
self.count=self.count+1
def do(self, X):#will hopefully colaborate all of the inputs
cont, num = True, 0
strstore="3 + 8"#temporarily used to make sure the calculating works
self.store=["2","1","+","2","3","4"]#holds the numbers used to calculate.
for num in range(1):
if X == "=":
cont = False
self.store[self.count]=X
print(self.store[self.count])
print(self.store[:])#test code
if cont == False:
print(self.eval_binary_expr(*(strstore.split())))
self.store=["2","1","+","2","3","4"]
If you initialize it this way in the do function, self.store will be reset to ["2","1","+","2","3","4"] every time you call do(X)
Initialize it outside of the do function if you don't want it to be overwritten.
But if you want to add a value to a list, use append(), extend, or += operator:
self.store+=["2","1","+","2","3","4"]
(if you want it to be done only once, do it in the constructor, __init__ function!)
also,
self.store[self.count]=X
If you try to append to the END of the list self.store, you should just do:
self.store.append(X)
This way, you won't need to count anything, with the risk of forgetting an incrementation and replacing a value by X instead of appending X.
As said above, range(1), it's 0...
Let's do it another way:
def do(self, X):
cont, num = True, 0
liststore=['3', '+', '8']#splitting your string.
#initialize str.store elsewhere!
if X == "=":
cont = False
self.store.append("X")
print(self.store[-1])#getting the last
print(self.store)#the same as self.store[:]
if cont == False:
print(self.eval_binary_expr(*(liststore)))
Simpler, and probably better.
Last thing: in Python, you are usually working with lists (like self.store), not arrays (which come from the module array)