-1

I am using tkinter library to write a form in python.

What I want is that I want the user to receive this message:

"please use Persian language"

if they start to fill the "نام" and "نام خانوادگی" field with English language.

actually I have the JavaScript function but I don't know how to code this in python with tkinter

this is the function for 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>

and here is the code I have used in python with 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()
ehsun
  • 137
  • 9
  • You can use the same regex, just re-write it to be legal Python. Eg, `r'^[\u0600-\u06FF\s]+$'`. But what's your actual question? Do you need to know how to tell your Entry widgets to call the Python version of the `just_persian` function? – PM 2Ring May 01 '18 at 15:36
  • BTW, it would be easier to help you if you reduced that code to a [mcve]. We don't really need to see all those widgets. – PM 2Ring May 01 '18 at 15:37
  • in the JavaScript function, when a non-Persian letter is used, an error will pop up and will ask for use of Persian language. I want the same thing to happen in the python code. if you could show me how to do that in my code I would be really thankfull – ehsun May 01 '18 at 16:11
  • this is how I call the function in my JavaScript code: – ehsun May 01 '18 at 16:13
  • Take a look at [Adding validation to an Entry widget](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry-validation.html). – PM 2Ring May 01 '18 at 16:18

1 Answers1

1

This would be the perfect place to make your own tiny subclass. There is a builtin tkinter validation protocol, but I find it a lot easier to simply trace the variable:

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

As you see I think the re is superfluous too; just use vanilla python to check the ord range.

Now simply replace Entry with Ehsun in every place that you want the Entry to be Persian-only:

e1 = Ehsun(left_frame, width=15)
e1.grid(row=1, column=0,padx=5, pady=5)
Novel
  • 13,406
  • 2
  • 25
  • 41