【发布时间】:2020-02-28 21:18:37
【问题描述】:
我在输入 Tkinter 条目时遇到问题。我做了一个注册码,我的代码上的每个条目都调用了一个虚拟键盘,虚拟键盘是独立的 python 程序。我成功调用了另一个 python 程序,但我无法使用我调用的虚拟键盘在我的主程序 Tkinter 条目上键入。 有没有办法做到这一点? 这是我的代码
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>',self.keyboard)
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>',self.keyboard)
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>',self.keyboard)
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>',self.keyboard)
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>',self.keyboard)
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()
def keyboard(self, event):
exec(open("keyboardclass.py").read());
这是我的keyboardclass.py
def select(value):
if value == "Space":
entry1.insert(tkinter.END, ' ')
elif value == "Backspace":
entry1.delete(len(entry1.get())-1,tkinter.END)
else:
entry1.insert(tkinter.END, value)
root = Tk()
root.configure(background = "cornflowerblue")
root.wm_attributes("-alpha", 0.7)
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']
Row = 2
Column = 0
for alphabet in alphabets:
command = lambda x=alphabet: select(x)
if alphabet != 'Space':
Button(root, text = alphabet,
command = command,width = 5, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column)
if alphabet == 'Enter':
Button(root, text = alphabet,
command = command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column, columnspan = 2)
if alphabet == 'Shift':
Button(root, text = alphabet,
command = command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column, columnspan = 2)
if alphabet == 'Space':
Button(root, text = alphabet,
command = command, width = 130, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = 6, columnspan = 16)
Column +=1
if Column > 13 and Row == 1:
Column = 0
Row += 1
if Column > 13 and Row == 2:
Column = 0
Row +=1
if Column > 13 and Row == 3:
Column = 0
Row +=1
if Column > 12 and Row == 4:
Column = 0
Row +=1
root.mainloop()
【问题讨论】:
-
keyboardclass.py中有什么?也许您应该更改它并将Entry作为键盘参数发送,以便它可以直接写入Entry。很久以前是类似的问题。如果我在 GitHub 上有这些问题的代码,我不记得了。 -
我的keyboardclass.py 中有一个Tkinter GUI 虚拟键盘。如果我把代码放在我的keyboardclass.py中它不会工作,我的意思是我可以在输入时完成它但是我产生了很多代码行,因为我将我的虚拟键盘方法分配给每个呈现的条目在我的主 GUI 上。这就是为什么我决定只从另一个程序调用虚拟键盘,但我无法让它在我的主程序上输入,因为它是分开的。
-
放学后我会研究你给我的东西。我没有得到足够的睡眠,非常渴望完成这件事。由于我不喜欢编程,所以我学得不是很快,python 是我知道的唯一编程语言,我上个月才开始,但我正在尽力而为。希望本周完成。太感谢了!非常感谢您的努力。非常感谢!
-
我不知道你在
keyboardclass.py中有什么,但你应该把它放在函数中并将它作为任何其他模块导入 -import keyboardclass- 然后从这个模块 - 即。keyboardclass.show_keyboard()。函数应该作为参数获取条目 - 即。keyboardclass.show_keyboard(self.entry_middle_name)并在函数内使用此条目来放置数据。
标签: python tkinter virtual-keyboard