验证不起作用

时间:2013-10-10 11:04:12

标签: python user-interface tkinter

我想确保如果没有输入数据,程序不只是打印和错误消息,而是要求用户完成测试。此外,我倾向于标记所有问题并在结果页面上打印结果。你能不能帮我解决这个问题,说有8个问题。我希望有三个乐队< = 2个版画你需要更努力4打印它的好,但你仍然需要练习,8个很棒。

from Tkinter import *
import tkMessageBox

app = Tk()
# Message Window

def messagePop():
    get_data()


# Background colour

app.configure(bg='gray')



# The position and size relative to the screen
app.geometry('500x500+450+140')

# The title of the program
app.title('Maths4Primary')

# The icon
app.wm_iconbitmap('MathIcon.ico')

# Object positioning in the program
# def GridPos:

# Q1 -- I might use the place() method for the screen layout.
Label(app, text="Q1",bg="white", fg="blue").place(x=15,y=10)

Label(app, text="Put these prices in order", bg="gray", fg="blue").place(x=100,y=10)

Label(app, text= u"\xA3" + "20.50", bg="gray", fg="blue").place(x=50,y=35)

Label(app, text=u"\xA3" + "2.50", bg="gray", fg="blue").place(x=200,y=35)

Label(app, text= u"\xA3" + "0.25", bg="gray", fg="blue").place(x=350,y=35)

# Entry







def get_data():
    global x_data,y_data,z_data,a_data
    a_data = float(a.get())
    x_data = float(x.get())
    y_data = float(y.get())
    z_data = float(z.get())

    print "x_data = {0} , y_data = {1} , z_data = {2}".format(x_data,y_data,z_data)

def messagePop():
    get_data()

    if (x_data==0.25) and (y_data==2.5) and (z_data==20.5) and (a_data==144):   
        print("Well done")
      #  tkMessageBox.showinfo('Results', '100% Very Good')


    elif (x_data!=0.25) and (y_data!=2.5) and (z_data!=20.5) and (a_data!=144):
        print("Please complete the test !")

    else:
        print("Something is incorrect")






# Defining the entry boxes

a = Entry(app)
x = Entry(app)
y = Entry(app)
z = Entry(app)

Label(app, text="Q2", bg="white", fg="blue").place(x=15,y=85)







Label(app, text="Calculate 336 - 192", bg="gray", fg="blue").place(x=100,y=85)



# Where the entry boxes are in the window
a.place(x=50,y=105)
x.place(x=50,y=60)
y.place(x=200,y=60)
z.place(x=350,y=60)

# Buttons
B1 = Button(app,text='Mark it',bg='gray99',fg='black', command = messagePop ).place(x=425,y=450)

app.mainloop()

1 个答案:

答案 0 :(得分:0)

由于此代码,您的代码失败了:

def get_data():
    global x_data,y_data,z_data,a_data
    a_data = float(a.get())
    x_data = float(x.get())
    y_data = float(y.get())
    z_data = float(z.get())

    print "x_data = {0} , y_data = {1} , z_data = {2}".format(x_data,y_data,z_data)

如果字段为空或者不包含数字,则当您尝试将字符串转换为float时,此函数将引发异常。您需要在该代码周围放置一个try / catch块,或者在调用此函数的代码周围放置一个try / catch块,这样您就可以捕获这些错误。