2013-10-10 41 views
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() 

回答

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塊,或者圍繞調用此函數的代碼,以便捕獲此類錯誤。