tkinter 按钮命令无需单击即可运行功能?

vin*_*nie 3 python tkinter python-3.x

我真的是 GUI 和 python 本身的新手。我正在尝试使用 tkinter 在 python 3.x 中制作一个简单的琐事游戏。这个想法是它会有多个问题,会告诉你你做对了还是错了,以及告诉你做对了多少。然而,我遇到的问题是,由于某种原因,我所有的按钮都表现得好像它们在没有被点击时被点击过一样。代码如下:

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()
Run Code Online (Sandbox Code Playgroud)

Eri*_*eil 9

choice1 = Button(master, text=question1[1].text, command=lambda : check(question1[1].value))
Run Code Online (Sandbox Code Playgroud)

在 command 之后,你必须给出一个函数(要么用 def before 定义,要么用 lambda 就地定义)。