user3763447
user3763447

Reputation: 29

tkinter entry validation not working

I've been working on getting the Entry box validation to work for the past few hours, I have tried editing everything around using 'self' like Bryan did here. ie:

self.root = tk.tk()

That left me with a single entry box which only allowed integers (which I want to only allow integers)

but adding the class and placing the main loop and 'calc = Tk()' (or self.root = tk.Tk() ) prevented my other widgets from being used.

So what I have provided is my current attempt which throws me this error:

TypeError: OnValidate() missing 1 required positional argument: 'W' Exception in Tkinter callback

Any help would be appreciated, ive searched the internet but it seems like there is little documentation on this method, or I am just really bad at researching.

Thanks for taking your time to read my question, I look forward to any answers.

from tkinter import *
import tkinter as tk
global choice 
choice = 0



def calculate(*event):
    if choice == 1:
        add1 = ccalc1.get()
        add2 = ccalc2.get()
        answ = add1 + add2         
        answer = Label(calc, text = answ)
        answer.grid(row=1, column=0)
    elif choice == 2:
        sub1 = ccalc1.get()
        sub2 = ccalc2.get()
        answ = sub1 - sub2         
        answer = Label(calc, text = answ)
        answer.grid(row=1, column=0)
    elif choice == 3:
        mul1 = ccalc1.get()
        mul2 = ccalc2.get()
        answ = mul1 * mul2         
        answer = Label(calc, text = answ)   
        answer.grid(row=1, column=0)
    elif choice == 4:
        div1 = ccalc1.get()
        div2 = ccalc2.get()
        answ = div1 / div2         
        answer = Label(calc, text = answ)
        answer.grid(row=1, column=0)
def choice1():
    global choice
    choice = 1  
    welcome.config(text="Addition")
def choice2():
    global choice
    choice = 2   
    welcome.config(text="Subtraction")
def choice3():
    global choice
    choice = 3   
    welcome.config(text="Multiplication")
def choice4():
    global choice
    choice = 4   
    welcome.config(text="Division")
tkinter     
def OnValidate(self, d, i, P, s, S, v, V, W):
        return S.isdigit()




calc = Tk()
calc.title("Calculator")
calc.geometry("200x140")





ccalc1 = IntVar()
ccalc2 = IntVar()



if choice == 0:
    welcome = Label(calc, text="Select a choice")
val = (calc.register(OnValidate),
      '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')  
calcbox1 = Entry(calc,textvariable=ccalc1, validate="key", 
                              validatecommand=val)
calcbox2 = Entry(calc,textvariable=ccalc2, validate="key", 
                              validatecommand=val)
submit = Button(calc, text="CALCULATE", command = calculate)

welcome.grid(row=0,column=0)
calcbox1.grid(row=2, column=0)
calcbox2.grid(row=3, column=0)
submit.grid(row=4, column=0)
calc.bind('<Return>', calculate)



menu=Menu(calc)

filemenu = Menu(menu,tearoff=0)
filemenu.add_command(label="Add", command = choice1)
filemenu.add_command(label="Subtract", command = choice2)
filemenu.add_command(label="Multiply", command = choice3)
filemenu.add_command(label="Divide", command = choice4)

menu.add_cascade(label="Operations",menu=filemenu)

help = Menu(menu,tearoff=0)
help.add_command(label="About")

menu.add_cascade(label="Help",menu=help)


calc.config(menu=menu)
calc.app = Frame(calc)
calc.app.grid()
calc.mainloop()  

Upvotes: 0

Views: 1514

Answers (1)

Kevin
Kevin

Reputation: 76254

Three problems:

  1. You have a NameError on line 49, just after the choice4 function.

        welcome.config(text="Division")
    tkinter      #what's this for?
    def OnValidate(self, d, i, P, s, S, v, V, W):
    

    Just delete the tkinter from that line.

  2. OnValidate should not have a self parameter, because it isn't part of a class.

    def OnValidate(d, i, P, s, S, v, V, W):
    
  3. An entry can't have a textvariable and a validatecommand at the same time. If you want a validate command, you'll have to do without the text variable. Everywhere that you use calc1.get() now, you'll have to replace with int(calcbox1.get()).

Upvotes: 4

Related Questions