我正在使用tkinter库在python中编写表单。
我想要的是我希望用户收到此消息:
“请使用波斯语”
如果他们开始用英语填写“نام”和“نامخانوادگی”字段。
实际上我有JavaScript函数,但我不知道如何使用tkinter
在python中编写代码这是JavaScript的功能:
<script>
function text(name)
{
var name = $(name).val();
if (name.length > 0) {
just_persian(name);
}
}
function just_persian(str) {
var p = /^[\u0600-\u06FF\s]+$/;
if (!p.test(str)) {
alert("please use Persian language");
}
}
</script>
这是我在python中使用tkinter
的代码from tkinter import *
root = Tk()
left_frame = Frame(root)
left_frame.grid(column=0)
label3 = Label(left_frame, text="ورود به سامانه", width=15)
label3.grid(row=0, column=0,columnspan=2, sticky=W + E + N + S)
label1 = Label(left_frame, text="شناسه کاربری", width=15)
label1.grid(row=1, column=1, sticky=W)
e1 = Entry(left_frame, width=15)
e1.grid(row=1, column=0,padx=5, pady=5)
label2 = Label(left_frame, text="رمز عبور", width=15)
label2.grid(row=2, column=1, sticky=W)
e2 = Entry(left_frame, width=15)
e2.grid(row=2, column=0,padx=5, pady=5)
button1=Button(left_frame,width=10, text="ورود")
button1.grid(row=3, column=0,sticky=W,pady=5,padx=5)
label4 = Label(left_frame, text="ثبت نام", width=15)
label4.grid(row=0, column=3,columnspan=2, sticky=W + E + N + S)
label5 = Label(left_frame, text="نام ", width=15)
label5.grid(row=1, column=4)
e3 = Entry(left_frame, width=15)
e3.grid(row=1, column=3,padx=5, pady=5)
label6 = Label(left_frame, text="نام خانوادگی ", width=15)
label6.grid(row=2, column=4)
e4 = Entry(left_frame, width=15)
e4.grid(row=2, column=3,padx=5, pady=5)
label7 = Label(left_frame, text="شماره دانشجویی ", width=15)
label7.grid(row=3, column=4)
e5 = Entry(left_frame, width=15)
e5.grid(row=3, column=3,padx=5, pady=5)
label8 = Label(left_frame, text="شناسه کاربری ", width=15)
label8.grid(row=4, column=4)
e6 = Entry(left_frame, width=15)
e6.grid(row=4, column=3,padx=5, pady=5)
label9 = Label(left_frame, text="رمز عبور ", width=15)
label9.grid(row=5, column=4)
e7 = Entry(left_frame, width=15)
e7.grid(row=5, column=3,padx=5, pady=5)
label10 = Label(left_frame, text="تکرار رمز عبور ", width=15)
label10.grid(row=6, column=4)
e8 = Entry(left_frame, width=15)
e8.grid(row=6, column=3,padx=5, pady=5)
label11 = Label(left_frame, text="رایانامه ", width=15)
label11.grid(row=7, column=4)
e9 = Entry(left_frame, width=15)
e9.grid(row=7, column=3,padx=5, pady=5)
label12 = Label(left_frame, text="سال ورود ", width=15)
label12.grid(row=8, column=4)
e10 = Entry(left_frame, width=15)
e10.grid(row=8, column=3,padx=5, pady=5)
label13 = Label(left_frame, text="استاد راهنما",width=15)
label13.grid(row=9, column=4)
OPTIONS = ["x", "y","z","w","v"]
variable = StringVar(root)
variable.set(OPTIONS[0]) # default value
w = OptionMenu(left_frame, variable, *OPTIONS)
w.grid(row=9,column=3,padx=5, pady=5)
button2=Button(left_frame,width=10, text="ثبت")
button2.grid(row=10, column=3,sticky=W,pady=5,padx=5)
button3=Button(left_frame,width=10, text="معتبر؟")
button3.grid(row=4, column=2,sticky=W,pady=5,padx=5)
root.mainloop()
答案 0 :(得分:1)
这将是制作自己的小子类的理想之地。有一个内置的tkinter验证协议,但我发现简单地跟踪变量要容易得多:
import tkinter as tk
from tkinter.messagebox import showerror
ALLOWED_ASCII = " " # spaces are not "persian" but they are allowed
class Ehsun(tk.Entry):
'''A type of Entry that only accepts Persion input'''
def __init__(self, master=None, **kwargs):
self.var = tk.StringVar()
self.old = ''
tk.Entry.__init__(self, master, textvariable=self.var, **kwargs)
self.var.trace('w', self.check)
def check(self, *args):
new_value = self.var.get()
if all(0x0600 <= ord(char) <= 0x06FF for char in new_value if char not in ALLOWED_ASCII):
self.old = new_value # all characters are Persian; ok to continue
else:
showerror("Errror", "please use Persian language")
self.var.set(self.old) # disallow, reset Entry to old value
如你所见,我觉得这也是多余的;只需使用vanilla python来检查ord范围。
现在,只需将Entry
替换为您希望条目仅为波斯语的每个地方的Ehsun
:
e1 = Ehsun(left_frame, width=15)
e1.grid(row=1, column=0,padx=5, pady=5)