【问题标题】:missing 1 required positional argument error in functions函数中缺少 1 个必需的位置参数错误
【发布时间】:2022-01-14 05:30:21
【问题描述】:
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()

由于某种原因,我收到以下错误:

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'

为什么会这样?我已经在 button_action() 中声明了参数“因素”。我也为函数提供了参数。这是否与我的代码放置有关?

【问题讨论】:

  • commandButton 选项的回调期望没有参数。

标签: python function tkinter


【解决方案1】:

您的 my_button 变量正在调用 button_action 函数。但是,该函数需要一个参数(在您的情况下命名为因子)。

您可以使用 lambda 函数修复它。

mybutton = Button(text='calculate', command=lambda: button_action(input_user.get())

您还需要 input_user.get()。因为这是从 tkinter 中的条目小部件获取数据的方法之一。

【讨论】:

    【解决方案2】:

    在 tkinter 参数command 中不接收任何参数。对于您想要的用途,您可以 1) 使用全局变量 2) 在函数中使用默认值 3) 使用 functools.partial(function,default_value)。这是一个删除了变量factor 的示例,因为它是radio_state 的副本。这里也使用了全局变量,您不需要使用任何参数调用button_action。也在这里修复错误:在启动时不设置默认模式,它会调用错误。

    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()
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 您是否忘记在button_action() 中返回saved_user_input?还是在button_action() 内更新converted['text']
    • @acw1668,有一些错误,如果我把它们都修好 7223이시윤,会失去一些经验
    猜你喜欢
    • 2019-10-30
    • 2023-03-22
    • 1970-01-01
    • 2020-10-12
    • 2020-08-13
    • 2014-09-13
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多