from tkinter import *
from math import *
window = Tk()
window.title('miles and km converter')
window.minsize('500', '300')
window.config(padx=20, pady=20)
input_user = Entry()
input_user.insert(END, string="0")
input_user.grid(row=0, column=1)
Label(text='is equal to').grid(row=1, column=0)
converted = Label(text='')
converted.grid(row=1, column=1)
def button_action(factor):
saved_user_input = round(float(input_user.get()) * float(factor), 2)
mybutton = Button(text='calculate', command=button_action)
mybutton.grid(row=2, column=1)
from_point = Label(text='')
from_point.grid(row=0, column=2)
to_point = Label(text='')
to_point.grid(row=1, column=2)
def to_miles():
from_point['text'] = 'km'
to_point['text'] = 'miles'
converted['text'] = button_action(factor=1)
def to_km():
from_point['text'] = 'miles'
to_point['text'] = 'km'
converted['text'] = button_action(factor=2)
radio_state = IntVar()
radio_1 = Radiobutton(text="km to miles", value=1,
variable=radio_state, command=to_miles)
radio_2 = Radiobutton(text="miles to km", value=2,
variable=radio_state, command=to_km)
radio_1.grid(row=3, column=3)
radio_2.grid(row=3, column=4)
window.mainloop()
For some reason, I am getting the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
TypeError: button_action() missing 1 required positional argument: 'factor'
Why is this happening? I already declared the argument 'factor' in my button_action(). I also provided parameters for the function too. Does this have something to do with the placement of my code?
CodePudding user response:
Your my_button
variable is calling button_action
function. However, the function requires an argument(named factor in your case).
You can fix it using the lambda function.
mybutton = Button(text='calculate', command=lambda: button_action(input_user.get())
You will also need the input_user.get(). Since this is one of the ways to get the data from an entry widget in tkinter.
CodePudding user response:
In tkinter parameter command
dont recive any argument. For use that you want you can 1) use global variable 2) use default value into function 3) use functools.partial(function,default_value)
. Here is a sample where varible factor
removed, becourse it is copy of radio_state
. Also here used global variable, and you dont need call button_action
with any parameter. Also here fix bug: on start dont set default mode and it calls errors.
from tkinter import *
from math import *
window = Tk()
window.title('miles and km converter')
window.minsize('500', '300')
window.config(padx=20, pady=20)
input_user = Entry()
input_user.insert(END, string="0")
input_user.grid(row=0, column=1)
Label(text='is equal to').grid(row=1, column=0)
converted = Label(text='')
converted.grid(row=1, column=1)
def button_action():
saved_user_input = round(float(input_user.get()) * float(radio_state.get()), 2) #replace factor bcrse radio_state used now
print(saved_user_input,radio_state.get()) #debug output
mybutton = Button(text='calculate', command=button_action)
mybutton.grid(row=2, column=1)
from_point = Label(text='')
from_point.grid(row=0, column=2)
to_point = Label(text='')
to_point.grid(row=1, column=2)
def to_miles():
from_point['text'] = 'km'
to_point['text'] = 'miles'
radio_state.set(1) #set mode
converted['text'] = button_action()
def to_km():
from_point['text'] = 'miles'
to_point['text'] = 'km'
radio_state.set(2)
converted['text'] = button_action()
radio_state = IntVar()
radio_state.set(1)#set default mode
radio_1 = Radiobutton(text="km to miles", value=1,
variable=radio_state, command=to_miles)
radio_2 = Radiobutton(text="miles to km", value=2,
variable=radio_state, command=to_km)
radio_1.grid(row=3, column=3)
radio_2.grid(row=3, column=4)
window.mainloop()