Python 3.x 如何处理pythonguizero/tkinter中的无字母键盘输入错误?
当我按tab键时,它会选择输入框中的所有文本,除非我将输入框多行设置为true,但它最终会给第一篇文章一种奇怪的格式。我想标签键的行为,它通常在任何其他程序。在shell中按Shift键或Caps lock键时,我也会出错 基于第一个键输入“if”语句是什么。Shift和Caps lock仍然可以正确显示文本 我曾尝试添加一个if语句来避免tab键错误,但它似乎不起作用。我试着使用pass和普通的print语句 我用的是什么Python 3.x 如何处理pythonguizero/tkinter中的无字母键盘输入错误?,python-3.x,tkinter,chatbot,keyboard-events,guizero,Python 3.x,Tkinter,Chatbot,Keyboard Events,Guizero,当我按tab键时,它会选择输入框中的所有文本,除非我将输入框多行设置为true,但它最终会给第一篇文章一种奇怪的格式。我想标签键的行为,它通常在任何其他程序。在shell中按Shift键或Caps lock键时,我也会出错 基于第一个键输入“if”语句是什么。Shift和Caps lock仍然可以正确显示文本 我曾尝试添加一个if语句来避免tab键错误,但它似乎不起作用。我试着使用pass和普通的print语句 我用的是什么 Python 3.7.9 IDE 3.3.4 基于tkinter
- Python 3.7.9
- IDE 3.3.4
- 基于tkinter
在按下
键_pressed
功能的开头添加此项将消除按下Caps Lock/Shift或返回空字符串的任何键时的错误
如果e.key=='':
返回
以下操作可防止Tab键选择文本
如果ord(e.key)==9:#制表键
打印('按下制表符')
输入框。追加(“”*4)
输入框。禁用()
输入框后(1,输入框启用)
基本上,我已经禁用了小部件,然后在1
毫秒后启用它
更新
另一种方法是将Tab键绑定到内部使用的Entry
小部件(可以使用中所述的tk
属性访问)。与前面的方法相比,我建议使用这种方法,因为append
方法在末尾添加文本,没有内置方法在当前位置插入文本,因此最终使用tkinter.Entry
的insert
方法,索引为'insert'
def选择_无(事件):
输入框.tk.insert('insert',''*4)
返回“中断”
输入\u box.tk.bind(“”,选择\u none)
在函数末尾使用
return'break'
可防止执行其他事件处理程序。如果e.key==,请尝试添加:在按下键的开始处返回None
“功能”选项卡仍然无法正常工作,除非启用了“多行”。您知道幕后使用的是什么小部件guizero
?它是一个tkinter.Entry
还是tkinter.Text
?输入框是一个条目,而文本只是说它的tkinter.label。这意味着guizero.TextBox
等同于tkinter.Text
?也可以使用“\t”
,而不是”*4
。有时更好,但有时不行。@TheLizzardguizero.TextBox.tk
相当于tkinter.Entry
它使用Entry
当多行为false时,我不明白为什么我的答案不起作用。如果你能先解释并亲自尝试,那就太好了。tab的第一个答案并不完全起作用。无法返回并将选项卡空间插入到已编写的文本中。Tk方法非常有效
from guizero import *
app = App(bg='#121212',title='Virtual assistant',width=500,height=500)
box=Box(app,width='fill',height=420)
box.bg='light gray'
Spacer_box=Box(box,width='fill',height=5,align='top')
userName='Test Username: '
#A is set as the default text value to test if posting into the box works
#Setting multiline to true changes the first post format but allows for the tab key to work properly
input_box = TextBox(app,text='A',width='fill',multiline=False,align="left")
input_box.text_size=15
input_box.bg='darkgray'
input_box.text_color='black'
input_box.tk.config(highlightthickness=5,highlightcolor="black")
def key_pressed(e):
#stop tab key from highlighting text only when input_box multiline is set to true
#in the input_box above
if ord(e.key) == 9: #Tab key
print('Tab pressed')
elif ord(e.key) == 13:#Enter key
#Checks if input box is blank or only white space.
if input_box.value.isspace() or len(input_box.value)==0:
print('Empty')
input_box.clear()
else:#User's output.
Textbox=Box(box,width='fill', align='top')
Usernamers=Text(Textbox, text=userName,align='left')
Usernamers.tk.config(font=("Impact", 14))
User_Outputted_Text = Text(Textbox, text=input_box.value,size=15,align='left')
print('Contains text')
input_box.clear()
#Test responce to user
if User_Outputted_Text.value.lower()=='hi':
Reply_box=Box(box,width='fill',align='top')
Digital_AssistantName=Text(Reply_box,text='AI\'s name:',align='left')
Digital_AssistantName.tk.config(font=("Impact", 14))
Reply_text = Text(Reply_box,text='Hello, whats up?',size=15,align='left')
input_box.when_key_pressed = key_pressed
app.display()