tkinter button命令运行功能而不点击?

时间:2015-05-08 17:15:59

标签: python python-3.x tkinter

我非常擅长GUI和python本身。我试图使用tkinter在python 3.x中制作一个简单的triva游戏。我的想法是,它会有多个问题,并会告诉你是否正确或错误,并告诉你你有多少是正确的。然而,我遇到的问题是,由于某些原因,所有按钮我都表现得就像他们没有点击时一样。代码如下:

from tkinter import *
class Correct(object):
    value = True
    def __init__(self, text):
        self.text = text


class Incorrect(object):
    value = False
    def __init__(self, text):
        self.text = text


def check(value):
    if value == True:
        print("you picked the right answer!")
    else:
        print("sorry thats not right")


question1 = ["this is a question", Correct("right answer"), Incorrect("wrong b"), Incorrect("wrong c"),
             Incorrect("wrong d")]


master = Tk()

qlabel1 = Label(master,text=question1[0])

# buttons
choice1 = Button(master, text=question1[1].text, command=check(question1[1].value))
choice2 = Button(master, text=question1[2].text, command=check(question1[2].value))
choice3 = Button(master, text=question1[3].text, command=check(question1[3].value))
choice4 = Button(master, text=question1[4].text, command=check(question1[3].value))

# pack
qlabel1.grid(row=0, column=0)
choice1.grid(row=1, column=0)
choice2.grid(row=1, column=2)
choice3.grid(row=2, column=0)
choice4.grid(row=2, column=1)
mainloop()

1 个答案:

答案 0 :(得分:1)

choice1 = Button(master, text=question1[1].text, command=lambda : check(question1[1].value))

在命令之后,你必须给出一个函数(使用def之前定义或使用lambda定义)。