在限制输入框输入数字字母时,我使用的是isalnum()方法,但是在输入时发现汉字也是可以输入进去的,不知道是什么原因。
代码如下:
import tkinter
from tkinter import *
class manager:
def __init__(self):
self.Window = tkinter.Tk()
self.Window.geometry("200x200+50+50") # heightxwidth+x+y
self.mainpanel = Canvas(self.Window, width=200, height=200) # main screen
self.mainpanel.pack()
entry_text = StringVar() # the text in your entry
entry_widget = Entry(self.mainpanel, width=20, textvariable=entry_text) # the entry
self.mainpanel.create_window(100, 100, window=entry_widget)
def test(content):
if content.isalnum() or content == "":
return True
else:
return False
lable1 = Label(self.Window, text="账号:", )
lable1.place(x=10, y=30)
v = StringVar()
test_cmd = self.Window.register(test)
textEntry1 = Entry(self.Window, bd=0, font=12, relief=RIDGE, bg="lightcyan",
textvariable=v,
validate="key",
validatecommand=(test_cmd, '%P')
)
textEntry1.place(x=45, y=30, width=130, height=25)
self.Window.mainloop()
if __name__ == '__main__':
app = manager()
运行图如下:
希望大家解决。