0

if 语句不起作用。即使您输入正确的值,它也会直接进入 else 并打印不正确。它会直接转到 else 并打印不正确的唯一原因是值不等于条件中的值。

from Tkinter import *
import tkMessageBox

app = Tk()
# Message Window

def messagePop():
    get_data()
    tkMessageBox.showinfo('Results', '100% Very Good')

# 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:

# I might use the place() method for the screen layout.
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






global x_data,y_data,z_data                       #----------add this

def get_data():
    global x_data,y_data,z_data
    x_data = x.get()
    y_data = y.get()
    z_data = z.get()
    print "x_data = {0} , y_data = {1} , z_data = {2}".format(x_data,y_data,z_data)

def messagePop():
    get_data()
    #---your Entry, which YOU NEED HELP ON THIS PART 
    if (x_data==0.25) and (y_data==2.5) and (z_data==20.5):   #----------compare here
        print("Well done")
      #  tkMessageBox.showinfo('Results', '100% Very Good')

    else :
        print ("Incorrect")










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

x.place(x=50,y=60)
y.place(x=200,y=60)
z.place(x=350,y=60)

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

app.mainloop()
4

1 回答 1

1

您正在将字符串与浮点值进行比较。它们永远不会相同,因为它们不是相同的基本类型。

与字符串比较:

if x_data == "0.25" and y_data == "2.5" and z_data == "20.5":

或 convert x_data, y_dataand z_datato 首先浮动。请注意,浮点比较也充满了问题,因为浮点数的精度有限。例如,请参阅Python 中的浮点相等性和一般情况。

于 2013-09-26T11:22:53.350 回答