我已经解决了一个多小时,但无法解决此问题。 我的错误是下面显示的链接。 [This is my error 我有两个班,我的主班和虚拟键盘班。 如果单击输入小部件,则必须弹出虚拟键盘。 我使用.bind,这是我的主类 init 中的代码。
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
master.title("Thesis")
self.login_button = Button(master, text = "Login",command = self.login, bg= "white")
self.login_button.pack()
self.register_button = Button(master, text="Register", command=self.register)
self.register_button.pack()
self.close_button = Button(master, text="Close", command=master.destroy)
self.close_button.pack()
self.db = pymysql.connect(host = "localhost",user = "root",passwd = "",db = "test")
self.cursor = self.db.cursor()
self.QueryResident = "CREATE TABLE IF NOT EXISTS residence (FIRST_NAME varchar(255) not null, MIDDLE_NAME varchar(255) not null, LAST_NAME varchar(255) not null,SEX varchar(255) not null, BIRTH_DATE varchar(255) not null, CIVIL_STATUS varchar(255) not null)"
self.cursor.execute(self.QueryResident)]
这是我在小部件上使用.bind时的代码。
def register(self):
self.master_register = Toplevel()
self.first_name = StringVar()
self.middle_name = StringVar()
self.last_name = StringVar()
self.sex = StringVar()
self.birth_day = StringVar()
self.civil_status = StringVar()
self.label_head = Label(self.master_register, text = "Please fill up all informations below")
self.label_head.pack()
self.label_first_name = Label(self.master_register, text = "First Name")
self.label_first_name.pack()
self.entry_first_name = Entry(self.master_register, textvariable = self.first_name)
self.entry_first_name.pack()
self.entry_first_name.bind('<ButtonPress-1>',keyboard.__init__)
self.label_middle_name = Label(self.master_register, text = "Middle Name")
self.label_middle_name.pack()
self.entry_middle_name = Entry(self.master_register, textvariable = self.middle_name)
self.entry_middle_name.pack()
self.entry_middle_name.bind('<ButtonPress-1>',keyboard.__init__)
self.label_last_name = Label(self.master_register, text = "Last Name")
self.label_last_name.pack()
self.entry_last_name = Entry(self.master_register, textvariable = self.last_name)
self.entry_last_name.pack()
self.entry_last_name.bind('<ButtonPress-1>',keyboard.__init__)
self.label_sex = Label(self.master_register, text = "Sex")
self.label_sex.pack()
self.radio_button_sex1 = Radiobutton(self.master_register, text = "Male", variable = self.sex, value = "Male" )
self.radio_button_sex1.pack()
self.radio_button_sex2 = Radiobutton(self.master_register, text = "Female", variable = self.sex, value = "Female")
self.radio_button_sex2.pack()
self.label_birthday = Label(self.master_register, text = "Birth Day")
self.label_birthday.pack()
self.entry_birthday = Entry(self.master_register, textvariable = self.birth_day)
self.entry_birthday.pack()
self.entry_birthday.bind('<ButtonPress-1>',keyboard.__init__)
self.label_civil_status = Label(self.master_register, text = "Civil Status")
self.label_civil_status.pack()
self.entry_civil_status = Entry(self.master_register, textvariable = self.civil_status)
self.entry_civil_status.pack()
self.entry_civil_status.bind('<ButtonPress-1>',keyboard.__init__)
self.button_submit = Button(self.master_register, text = "Submit", command = lambda: self.registered(self.first_name, self.middle_name,
self.last_name, self.sex, self.birth_day,
self.civil_status))
self.button_submit.pack()
这是我的虚拟键盘课程。 “报亭”是我的主要班级名称。
class keyboard(Kiosk):
def __init__(self, master, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.vkeyboard = master
vkeyboard.title("Virtual Keyboard")
self.lowercase()
def selectlower(self, value):
if value == "Space":
self.entry.insert(tkinter.END, ' ')
elif value == "<-Backspace":
self.entry.delete(len(self.entry.get())-1,tkinter.END)
elif value == "Caps Lock":
self.uppercase()
else:
self.entry.insert(tkinter.END, value)
def selectupper(self, value):
if value == "Space":
self.entry.insert(tkinter.END, ' ')
elif value == "<-Backspace":
self.entry.delete(len(self.entry.get())-1,tkinter.END)
elif value == "Caps Lock":
self.lowercase()
else:
self.entry.insert(tkinter.END, value)
def uppercase(self):
self.ALPHABETS = ['`','1','2','3','4','5','6','7','8','9','0','-','=','Backspace',
'Tab','Q','W','E','R','T','Y','U','I','O','P','[',']',"\\",
'Caps Lock','A','S','D','F','G','H','J','K','L',';',"'",'Enter',
'Shift','Z','X','C','V','B','N','M',',','.','/','Shift',
'Space']
self.Row = 2
self.Column = 0
for self.alphabet in self.ALPHABETS:
self.command = lambda x=self.alphabet: self.selectupper(x)
if self.alphabet != 'Space':
Button(self.vkeyboard, text = self.alphabet,
command = self.command,width = 5, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = self.Row, column = self.Column)
if self.alphabet == 'Enter':
Button(self.vkeyboard, text = self.alphabet,
command = self.command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = self.Row, column = self.Column, columnspan = 2)
if self.alphabet == 'Shift':
Button(self.vkeyboard, text = self.alphabet,
command = self.command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = self.Row, column = self.Column, columnspan = 2)
if self.alphabet == 'Space':
Button(self.vkeyboard, text = self.alphabet,
command = self.command, width = 130, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = 6, columnspan = 16)
self.Column +=1
if self.Column > 13 and self.Row == 1:
self.Column = 0
self.Row += 1
if self.Column > 13 and self.Row == 2:
self.Column = 0
self.Row +=1
if self.Column > 13 and self.Row == 3:
self.Column = 0
self.Row +=1
if self.Column > 12 and self.Row == 4:
self.Column = 0
self.Row +=1
def lowercase(self,event):
self.entry = Entry(self.vkeyboard, text = "input")
self.entry.grid(row =1, columnspan = 40)
self.alphabets = ['`','1','2','3','4','5','6','7','8','9','0','-','=','Backspace',
'Tab','q','w','e','r','t','y','u','i','o','p','[',']',"\\",
'Caps Lock','a','s','d','f','g','h','j','k','l',';',"'",'Enter',
'Shift','z','x','c','v','b','n','m',',','.','/','Shift',
'Space']
self.Row = 2
self.Column = 0
for self.alphabet in self.alphabets:
self.command = lambda x=self.alphabet: self.selectlower(x)
if self.alphabet != 'Space':
Button(self.vkeyboard, text = self.alphabet,
command = self.command,width = 5, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = self.Row, column = self.Column)
if self.alphabet == 'Enter':
Button(self.vkeyboard, text = self.alphabet,
command = self.command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = self.Row, column = self.Column, columnspan = 2)
if self.alphabet == 'Shift':
Button(self.vkeyboard, text = self.alphabet,
command = self.command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = self.Row, column = self.Column, columnspan = 2)
if self.alphabet == 'Space':
Button(self.vkeyboard, text = self.alphabet,
command = self.command, width = 130, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = 6, columnspan = 16)
self.Column +=1
if self.Column > 13 and self.Row == 1:
self.Column = 0
self.Row += 1
if self.Column > 13 and self.Row == 2:
self.Column = 0
self.Row +=1
if self.Column > 13 and self.Row == 3:
self.Column = 0
self.Row +=1
if self.Column > 12 and self.Row == 4:
self.Column = 0
self.Row +=1
我认为我的问题是继承或处理程序,这是我第一次进行继承并调用另一个类。我的主类没有任何问题,我的数据库和注册功能运行正常。但是,当我将虚拟键盘添加为另一个类时,出现了此错误。 希望你们能帮助我。 非常感谢!