2014-09-19 49 views
0

我一直在努力讓Entry Box驗證在過去的幾個小時裏工作,我試着編輯使用像Bryan那樣的'self'做的所有事情都做了here。 即:tkinter入口驗證不起作用

self.root = tk.tk()

這給我留下了一個輸入框,只容許整數(我想只允許整數)

但添加類和放置主循環和'calc = Tk()'(或self.root = tk.Tk())可以防止我的其他小部件被使用。

所以我提供的是我會拋出我這個錯誤當前的嘗試:

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

任何幫助,將不勝感激,我已經在網上搜索,但它好像有這種方法的文檔很少,或者我我在研究方面真的很糟糕。

感謝您花時間閱讀我的問題,我期待着任何答案。

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() 
+0

您還沒有經過適當的'self'參數'OnValidate'(這並不似乎是一個方法反正),所以一切會向上移動,你似乎缺少'W'。 – jonrsharpe 2014-09-19 15:09:30

+0

爲了擴展jon的評論,除非該函數在類中,否則第一個參數爲self的函數沒有任何意義。這是你的代碼和Bryan的區別。 – Kevin 2014-09-19 15:12:11

+0

那麼我該如何正確地通過它?我嘗試了很多方法,但其中每個人都失敗了。我會在哪裏錯過'W'? 'val'和'onValidate'都有'W' – user3763447 2014-09-19 15:13:33

回答

3

三個問題:

  1. 你對線49 NameError,只是choice4功能之後。

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

    只需從該行刪除tkinter即可。

  2. OnValidate不應該有self參數,因爲它不是類的一部分。

    def OnValidate(d, i, P, s, S, v, V, W): 
    
  3. 的條目不能有textvariablevalidatecommand在同一時間。如果你想要一個驗證命令,你必須不使用文本變量。無論您現在使用calc1.get(),都必須使用int(calcbox1.get())替換。

+0

謝謝凱文。我忘了我能夠直接從輸入框中獲得。 名稱錯誤是一個意外的安置,我已經從代碼中刪除它。 非常感謝您的幫助。 – user3763447 2014-09-19 15:32:11