How to validate canvas text in python?

Viewed 51

I want to restrict the text box to characters only and Incase of error it should show messagebox

Also text box should show alternate characters but its showing only A

This is the function I used but it's not working

  def check():
    sel = T.get()
    
    if not sel.isalpha():
        messagebox.showerror('Only letters','Only letters are allowed!')

below is my code:

from tkinter import *
root = Tk()
root.geometry("1600x890")

def check():
    sel = T.get("1")
    l = T1.get("1")

    if sel != 0 & l !=0:
    messagebox.showerror('Only letters','Only letters are allowed!')
    else:
        root.destroy()
        import mpr02


bg = PhotoImage(file="C:\\New folder (2)\\Pictures\python proj\\1.png")

canvas1 = Canvas(root, width=400, height=400)

canvas1.pack(fill="both", expand=True)

canvas1.create_image(0, 0, image=bg, anchor="nw")

canvas1.create_text(760, 60, text="WELCOME TO THE NARWANI ATLANTA", font=('Times New Roman', '24', 'bold'))
label2 = Label(root, text="USERNAME", fg="blue", bg="cyan")
label1 = Label(root, text="PASSWORD", fg="blue", bg="cyan")
T1 = Text(root, height=2.4, width=20)
button1 = Button(root, text="LOGIN", bg="magenta", fg="cyan", command=check)

button1_canvas = canvas1.create_window(744, 600, window=button1, width=90, height=38)
T = Text(root, height=2.4, width=20)
h="Adca"
T1 = Entry(root,show=h);
T_canvas = canvas1.create_window(850, 358, window=T)
label2_canvas = canvas1.create_window(624, 355, window=label2, width=138, height=37)
label1_canvas = canvas1.create_window(624, 455, window=label1, width=138, height=37)
T1_canvas = canvas1.create_window(850, 458, window=T1)
root.mainloop()
1 Answers

You're not calling Text widget get() method with the proper arguments — see Methods on Text widgets. You can snatch its entire contents from beginning to end using the text indices shown in the snippet below:

from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry("1600x890")

def check():
    sel = T.get('1.0', END)
    if not sel.isalpha():
        messagebox.showerror('Only letters','Only letters are allowed!')

bg = PhotoImage(file="8-ball.png")

canvas1 = Canvas(root, width=400, height=400)
canvas1.pack(fill="both", expand=True)

canvas1.create_image(0, 0, image=bg, anchor="nw")
canvas1.create_text(760, 60, text="Welcome to the Narwani Atlanta",
                    font=('Times New Roman', '24', 'bold'))

label2 = Label(root, text="USERNAME", fg="blue", bg="cyan")
label2_canvas = canvas1.create_window(624, 355, window=label2, width=138, height=37)

label1 = Label(root, text="PASSWORD", fg="blue", bg="cyan")
label1_canvas = canvas1.create_window(624, 455, window=label1, width=138, height=37)

button1 = Button(root, text="LOGIN", bg="magenta", fg="cyan", command=check)
button1_canvas = canvas1.create_window(744, 600, window=button1, width=90, height=38)

T = Text(root, height=2.4, width=20)
T_canvas = canvas1.create_window(850, 358, window=T)

T1 = Entry(root, show="*");
T1_canvas = canvas1.create_window(850, 458, window=T1)

root.mainloop()

Related