why am I getting this TypeError: () got an unexpected keyword argument error?
why am I getting this TypeError: () got an unexpected keyword argument error?
出于某种原因,我试图将 tkinter
图形添加到我的一小部分代码中,并希望 window.How 上的输出(计算总和)我是否使此代码正常工作请帮忙!
我的代码:
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry('400x600')
window.resizable(0, 0)
window.title("HACKER-simple interest setup")
# define entry variables
n1 = StringVar()
n2 = StringVar()
n3 = StringVar()
def simple_interest(*x):
global principal, time
principal = float(principal.get())
time = float(time.get())
interest_rate = float(ir.get())
# simple interest calculating engine
simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
Output = Text(window, width=25, bg="light cyan")
Output.grid(column=0, row=4)
Output.place(x=150, y=300)
Output.insert(END, "Simple_Interest:", simple_interest)
lbl = Label(window, text="Principal:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=45, y=125)
lbl = Label(window, text="Time:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=70, y=150)
lbl = Label(window, text="Interest rate:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=12, y=170)
principal = tk.Entry(window, textvariable=n1, width=40)
principal.grid(column=0, row=4)
principal.place(x=130, y=130)
time = tk.Entry(window, textvariable=n2, width=40)
time.grid(column=0, row=4)
time.place(x=130, y=150)
ir = tk.Entry(window, textvariable=n3, width=40)
ir.grid(column=0, row=4)
ir.place(x=130, y=170)
btn = Button(window, text="Calculate", bg="red", fg="white", command=simple_interest)
btn.grid(column=0, row=5)
btn.place(x=220, y=200)
window.mainloop()
获取错误:
C:\Users\Anmol\AppData\Local\Programs\Python\Python38\python.exe "D:/Downloads/si test.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Anmol\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "D:/Downloads/si test.py", line 22, in simple_interest
simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
TypeError: simple_interest() got an unexpected keyword argument 'Simple_Interest'
更改第 22 行 simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
至 simple_interest=principal * (interest_rate / 100) * time
将第 27 行 Output.insert(END, "Simple_Interest:", simple_interest)
更改为 Output.insert(END, "Simple_Interest:" + str(simple_interest))
我是怎么解决的?
我将单利分配给变量单利
对于输出,它只是将 'simple interest' 打印为字符串,所以我将其替换为结果
我想你可以在第 22 行使用 Simple_Interest=principal * (interest_rate / 100) * time
。
您应该在第 17-19 行使用“if”语句
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry('400x600')
window.resizable(0, 0)
window.title("HACKER-simple interest setup")
# define entry variables
n1 = StringVar()
n2 = StringVar()
n3 = StringVar()
def simple_interest(*x):
global principal, time
principal = float(principal.get() if not isinstance(principal,float) else principal)
time = float(time.get()) if not isinstance(time,float) else time
interest_rate = float(ir.get()) if not isinstance(ir,float) else ir
# simple interest calculating engine
Simple_Interest=principal * (interest_rate / 100) * time
Output = Text(window, width=25, bg="light cyan")
Output.grid(column=0, row=4)
Output.place(x=150, y=300)
Output.insert(END, "Simple_Interest:\n"+str(Simple_Interest))
lbl = Label(window, text="Principal:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=45, y=125)
lbl = Label(window, text="Time:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=70, y=150)
lbl = Label(window, text="Interest rate:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=12, y=170)
principal = tk.Entry(window, textvariable=n1, width=40)
principal.grid(column=0, row=4)
principal.place(x=130, y=130)
time = tk.Entry(window, textvariable=n2, width=40)
time.grid(column=0, row=4)
time.place(x=130, y=150)
ir = tk.Entry(window, textvariable=n3, width=40)
ir.grid(column=0, row=4)
ir.place(x=130, y=170)
btn = Button(window, text="Calculate", bg="red", fg="white", command=simple_interest)
btn.grid(column=0, row=5)
btn.place(x=220, y=200)
window.mainloop()
成功了!
出于某种原因,我试图将 tkinter
图形添加到我的一小部分代码中,并希望 window.How 上的输出(计算总和)我是否使此代码正常工作请帮忙!
我的代码:
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry('400x600')
window.resizable(0, 0)
window.title("HACKER-simple interest setup")
# define entry variables
n1 = StringVar()
n2 = StringVar()
n3 = StringVar()
def simple_interest(*x):
global principal, time
principal = float(principal.get())
time = float(time.get())
interest_rate = float(ir.get())
# simple interest calculating engine
simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
Output = Text(window, width=25, bg="light cyan")
Output.grid(column=0, row=4)
Output.place(x=150, y=300)
Output.insert(END, "Simple_Interest:", simple_interest)
lbl = Label(window, text="Principal:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=45, y=125)
lbl = Label(window, text="Time:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=70, y=150)
lbl = Label(window, text="Interest rate:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=12, y=170)
principal = tk.Entry(window, textvariable=n1, width=40)
principal.grid(column=0, row=4)
principal.place(x=130, y=130)
time = tk.Entry(window, textvariable=n2, width=40)
time.grid(column=0, row=4)
time.place(x=130, y=150)
ir = tk.Entry(window, textvariable=n3, width=40)
ir.grid(column=0, row=4)
ir.place(x=130, y=170)
btn = Button(window, text="Calculate", bg="red", fg="white", command=simple_interest)
btn.grid(column=0, row=5)
btn.place(x=220, y=200)
window.mainloop()
获取错误:
C:\Users\Anmol\AppData\Local\Programs\Python\Python38\python.exe "D:/Downloads/si test.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Anmol\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "D:/Downloads/si test.py", line 22, in simple_interest
simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
TypeError: simple_interest() got an unexpected keyword argument 'Simple_Interest'
更改第 22 行 simple_interest(Simple_Interest=principal * (interest_rate / 100) * time)
至 simple_interest=principal * (interest_rate / 100) * time
将第 27 行 Output.insert(END, "Simple_Interest:", simple_interest)
更改为 Output.insert(END, "Simple_Interest:" + str(simple_interest))
我是怎么解决的?
我将单利分配给变量单利
对于输出,它只是将 'simple interest' 打印为字符串,所以我将其替换为结果
我想你可以在第 22 行使用 Simple_Interest=principal * (interest_rate / 100) * time
。
您应该在第 17-19 行使用“if”语句
import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry('400x600')
window.resizable(0, 0)
window.title("HACKER-simple interest setup")
# define entry variables
n1 = StringVar()
n2 = StringVar()
n3 = StringVar()
def simple_interest(*x):
global principal, time
principal = float(principal.get() if not isinstance(principal,float) else principal)
time = float(time.get()) if not isinstance(time,float) else time
interest_rate = float(ir.get()) if not isinstance(ir,float) else ir
# simple interest calculating engine
Simple_Interest=principal * (interest_rate / 100) * time
Output = Text(window, width=25, bg="light cyan")
Output.grid(column=0, row=4)
Output.place(x=150, y=300)
Output.insert(END, "Simple_Interest:\n"+str(Simple_Interest))
lbl = Label(window, text="Principal:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=45, y=125)
lbl = Label(window, text="Time:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=70, y=150)
lbl = Label(window, text="Interest rate:", font=("Aerial Bold Italic", 15))
lbl.grid(column=0, row=4)
lbl.place(x=12, y=170)
principal = tk.Entry(window, textvariable=n1, width=40)
principal.grid(column=0, row=4)
principal.place(x=130, y=130)
time = tk.Entry(window, textvariable=n2, width=40)
time.grid(column=0, row=4)
time.place(x=130, y=150)
ir = tk.Entry(window, textvariable=n3, width=40)
ir.grid(column=0, row=4)
ir.place(x=130, y=170)
btn = Button(window, text="Calculate", bg="red", fg="white", command=simple_interest)
btn.grid(column=0, row=5)
btn.place(x=220, y=200)
window.mainloop()
成功了!