我正在使用tkinter和customtkinter开发一个GUI程序。在尝试创建这个popUpBox时,我似乎遇到了一个恼人的错误。重要的是,我在脚本运行期间使用它打开带有单选按钮的弹出框以进行选择。如果我在主循环中运行它,它就会正常工作,但是如果我尝试在主GUI中运行它,它就会崩溃,如果我试图以topLevel的形式运行它,我会得到底部的错误。
import tkinter
import customtkinter
class equipPopUp(customtkinter.CTkToplevel):
def __init__(self,titleName,staff,labelFound=''):
super().__init__()
self.rfrBTNList=[]
self.printBTNList=[]
self.labelFound = labelFound
self.staff = staff
if self.staff == 0:
self.status = 'Student'
elif self.staff == 1:
self.status = 'Staff'
self.titleName = titleName
#### Main Window ###
self.title(f"{titleName}")
window_width = 750
window_height = 600
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
# find the center point
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
self.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
self.btn1 = tkinter.StringVar()
self.btn2 = tkinter.StringVar()
self.btn3 = tkinter.StringVar()
self.label_radio_group = customtkinter.CTkLabel(master=self,
text="Please Select Request Equipment:")
self.label_radio_group.grid(row=0,column=0,sticky='nw')
if self.labelFound == 'N':
z = 0
Equipment_Requested = ["Print Return Label at SCA", "Email Electronic Return Label", "Email Electronic Return Label"]
for i in Equipment_Requested:
z+=1
self.radio_button = customtkinter.CTkRadioButton(master=self,
text= i,
variable=self.btn3,
value=z)
self.radio_button.grid(row=z,column=0,sticky='nw')
x = 0
Equipment_Requested = [f"Replacement {self.status} Kit", f"Replacement {self.status} Printer", "Charger"]
for i in Equipment_Requested:
x+=1
self.radio_button = customtkinter.CTkRadioButton(master=self,
text= i,
variable=self.btn1,
value=x,
command=self.submitButton)
self.radio_button.grid(row=x,column=1,sticky='nw')
else:
x = 0
Equipment_Requested = [f"Replacement {self.status} Kit", f"Replacement {self.status} Printer", "Charger"]
for i in Equipment_Requested:
x+=1
self.radio_button = customtkinter.CTkRadioButton(master=self,
text= i,
variable=self.btn1,
value=x,
command=self.submitButton)
self.radio_button.grid(row=x,column=0,sticky='nw')
def createLabelBtns(self):
y=0
ra=self.label_radio_group2 = customtkinter.CTkLabel(master=self,
text="Please Select Reason for Return:")
self.label_radio_group2.grid(row=0,column=2,sticky='nw')
self.rfrBTNList.append(ra)
rlm = ["Print Return Label at SCA", "Email Electronic Return Label", "Email Electronic Return Label"]
for i in rlm:
y+=1
rb=self.radio_button = customtkinter.CTkRadioButton(master=self,
text= i,
variable=self.btn3,
value=y)
self.radio_button.grid(row=y,column=2,sticky='nw')
self.rfrBTNList.append(rb)
def createRFRBtns(self):
y=0
ra=self.label_radio_group2 = customtkinter.CTkLabel(master=self,
text="Please Select Reason for Return:")
self.label_radio_group2.grid(row=0,column=2,sticky='nw')
self.rfrBTNList.append(ra)
equipment_reason_for_return = ["Display", "OS/MB", "Keyboard", "Camera", "Audio/Mic", "Battery", "Physical Damage"]
for i in equipment_reason_for_return:
y+=1
rb=self.radio_button = customtkinter.CTkRadioButton(master=self,
text= i,
variable=self.btn2,
value=y)
self.radio_button.grid(row=y,column=2,sticky='nw')
self.rfrBTNList.append(rb)
self.my_button = customtkinter.CTkButton(master=self, text="Submit", command = self.destroy)
self.my_button.grid(row=8,column=2,sticky='nw')
def createPrinterChoices(self):
y=0
ra=self.label_radio_group3 = customtkinter.CTkLabel(master=self,
text="Please Select Reason for Return of Printer:")
self.label_radio_group3.grid(row=0,column=2,sticky='nw')
self.printBTNList.append(ra)
equipment_reason_for_return = ["Hardware", "Software"]
for i in equipment_reason_for_return:
y+=1
rb=self.radio_button = customtkinter.CTkRadioButton(master=self,
text= i,
variable=self.btn2,
value=y)
self.radio_button.grid(row=y,column=2,sticky='nw')
self.printBTNList.append(rb)
self.my_button = customtkinter.CTkButton(master=self, text="Submit", command = self.destroy)
self.my_button.grid(row=8,column=2,sticky='nw')
def submitButton(self):
if self.btn1.get()=='1':
for widget in self.printBTNList:
widget.grid_remove()
self.createRFRBtns()
if self.btn1.get()=='2':
for widget in self.rfrBTNList:
widget.grid_remove()
self.createPrinterChoices()
if self.btn1.get()=='3':
for widget in self.rfrBTNList:
widget.grid_remove()
for widget in self.printBTNList:
widget.grid_remove()
self.my_button = customtkinter.CTkButton(master=self, text="Submit", command = self.returnStuff)
self.my_button.grid(row=8,column=2,sticky='nw')
def submitButton2(self):
self.destroy
def printSubmitBTN(self):
# print("Printer"+self.btn2.get())
pass
def returnStuff(self):
return self.btn1.get(),self.btn2.get(),self.btn3.get()
def start(self):
self.mainloop()
print(type(self.btn1.get()))
print(type(self.btn2.get()))
print(type(self.btn3.get()))
return self.btn1.get(),self.btn2.get(),self.btn3.get()我收到的错误是
rlm,ERI,RFRI = equipPopUp('Equipment Requested',staff,'N')
File "C:\Program Files\Python39\lib\tkinter\__init__.py", line 1652, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: can only concatenate str (not "int") to str我在用
staff=1
rlm,ERI,RFRI = equipPopUp('Equipment Requested',staff,'N')我不知道是什么引起的。我已经研究了这个特别的错误,从我收集到的信息来看,我应该尝试把str和int混合在一起,但是我只是不知道它在哪里做的。我正在使用python版本3.9.13
发布于 2022-06-08 22:26:48
问题是这条线:
rlm,ERI,RFRI = equipPopUp('Equipment Requested',staff,'N')我不知道为什么你认为equipPopUp(...)会返回一个元组。因为它是一个类,所以它返回一个对象。试图将结果转换为元组将导致错误。
在这段代码的第二行中,您可以得到完全相同的错误:
popup = equipPopUp('Equipment Requested',staff,'N')
rlm,ERI,RFRI = popup因此,为了消除错误,不要尝试将equipPopUp实例转换为元组。
https://stackoverflow.com/questions/72552465
复制相似问题