0

我想确保如果没有输入数据,程序不只是打印和错误消息,而是要求用户完成测试。此外,我倾向于标记所有问题并将结果打印在结果页面上。你能帮我解决这个问题吗,说有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()
4

1 回答 1

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)

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

于 2013-10-17T13:46:42.033 回答