我有一些代碼,我打算在每次用戶對 tkinter 小部件進行更改時運行計算。如果您單擊編輯框或更改下拉框,則功能運行良好。我遇到的問題是我無法讓 CheckBox 的命令列接受這些功能。當代碼適用于所有其他小部件時,它會拋出“缺少 1 個必需的位置引數:'self'”。誰能讓我知道我做錯了什么?下面的代碼:
#Import everything from tkinter, messageboxes, ttk and math
from tkinter import *
from tkinter import ttk
Form1=Tk()
# Calculate Variables - Main function. Run every time a change is made to the form
def CalcVariables(Msg):
Msg = "Run Calculations"
print(Msg)
#Run Multiple Funtions - Tied to command lines of box functionality
def MultipleFunctions(*funcs):
def FuncLoop(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return FuncLoop
#Check Length Box entry is Valid
def LthCheck(Msg):
#Check entry is numeric - give warning if not and exit
try:
float(Lth.get())
except:
Msg = "Length box only numbers."
print(Msg)
Lth.focus()
return
#Check Width Box Entry is Valid
def WthCheck(Msg):
#Check entry is numeric - give warning if not and exit
try:
int(Wth.get())
except ValueError:
Msg = "Width box only accepts numbers."
print(Msg)
Wth.focus()
return
#Length EditBox
Lth = Entry(Form1,width=10)
Lth.grid(row=0, column=1, sticky=W)
Lth.insert(0,10)
Lth.bind("<FocusOut>",MultipleFunctions(LthCheck, CalcVariables))
Label (Form1, text="Length") .grid(row=0, column=0, sticky=W)
#Width EditBox
Wth = Entry(Form1,width=10)
Wth.grid(row=1, column=1, sticky=W)
Wth.insert(0,1)
Wth.bind("<FocusOut>",MultipleFunctions(WthCheck, CalcVariables))
Label (Form1, text="Width") .grid(row=1, column=0, sticky=W)
#Type DropDownBox
Type = [
"Type 1",
"Type 2",
]
PartStyle = StringVar()
PartStyle.set(Type[0])
PartStyleDrop = OptionMenu(Form1, PartStyle, *Type, command=MultipleFunctions(LthCheck, WthCheck, CalcVariables))
PartStyleDrop.grid(row=3, column=1,sticky=W)
Label (Form1, text="Part") .grid(row=3, column=0, sticky=W)
#Check Button
MT = IntVar()
ModType = Checkbutton(Form1, text = "Modify", variable = MT, onvalue = 1, offvalue =0, command= MultipleFunctions(LthCheck, WthCheck))
ModType.grid(row=4,column=0)
Lth.focus()
Form1.mainloop()
uj5u.com熱心網友回復:
該錯誤表明您正在嘗試呼叫一個函式,但沒有將所需的引數傳遞給它self。您實際上不需要self任何這些函式的引數。self僅在類的方法中是必需的。事實上,這些函式不需要引數,當將函式的引數串列留空時,錯誤就解決了,例如
def LthCheck():
...
編輯:但是,如評論中所述,這將導致錯誤,例如“LthCheck() 采用 0 個位置引數,但給出了 1 個”。這是由于對 CheckButton 使用 bind 進行互動和命令的不同。使用小部件的 .bind 方法系結到事件時使用的函式將被賦予一個引數。這通常包含有關事件的資訊。作為 CheckButton 的命令引數傳遞的函式將不會傳遞任何引數。因此,您可以將函式系結到 ModType 而不是將它們作為命令引數傳遞,以避免這些錯誤。在這種情況下,這些功能需要一個引數,例如事件。
以下是代碼的外觀,我相信它可以解決所有當前問題:
#Import everything from tkinter, messageboxes, ttk and math
from tkinter import *
from tkinter import ttk
Form1=Tk()
# Calculate Variables - Main function. Run everytime a change is made to the form
def CalcVariables(event):
print("Run Calculations")
#Run Multiple Funtions - Tied to command lines of box functionality
def MultipleFunctions(*funcs):
def FuncLoop(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return FuncLoop
#Check Length Box entry is Valid
def LthCheck(event):
#Check entry is numeric - give warning if not and exit
try:
float(Lth.get())
except:
print("Length box only numbers.")
Lth.focus()
return
#Check Width Box Entry is Valid
def WthCheck(event):
#Check entry is numeric - give warning if not and exit
try:
int(Wth.get())
except ValueError:
print("Width box only accepts numbers.")
Wth.focus()
return
#Length EditBox
Lth = Entry(Form1,width=10)
Lth.grid(row=0, column=1, sticky=W)
Lth.insert(0,10)
Lth.bind("<FocusOut>", MultipleFunctions(LthCheck, CalcVariables))
Label(Form1, text="Length").grid(row=0, column=0, sticky=W)
#Width EditBox
Wth = Entry(Form1,width=10)
Wth.grid(row=1, column=1, sticky=W)
Wth.insert(0,1)
Wth.bind("<FocusOut>",MultipleFunctions(WthCheck, CalcVariables))
Label(Form1, text="Width").grid(row=1, column=0, sticky=W)
#Type DropDownBox
Type = [
"Type 1",
"Type 2",
]
PartStyle = StringVar()
PartStyle.set(Type[0])
PartStyleDrop = OptionMenu(Form1, PartStyle, *Type, command=MultipleFunctions(LthCheck, WthCheck, CalcVariables))
PartStyleDrop.grid(row=3, column=1,sticky=W)
Label (Form1, text="Part") .grid(row=3, column=0, sticky=W)
#Check Button
MT = IntVar()
ModType = Checkbutton(Form1, text = "Modify", variable = MT, onvalue = 1, offvalue =0)
ModType.bind("<ButtonRelease>", MultipleFunctions(LthCheck, WthCheck))
ModType.grid(row=4,column=0)
Lth.focus()
Form1.mainloop()
值得注意的是,在處理此代碼時,在LthCheckandWthCheck函式中添加一條列印陳述句以查看在選中復選框時實際呼叫了它們可能會有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490463.html
