简体   繁体   English

如何在python中从tkinter接受按钮输入?

[英]How can I take button input from tkinter in python?

I have this game where you need to choose the text and not the color it is in: 我有一个游戏,您需要选择文本而不是颜色:

import tkinter, os, random

colors = ['Green', 'Red', 'Blue','Yellow', 'Pink', 'Orange', 'Purple', 'Grey', 'Brown', 'Black']

window = tkinter.Tk()
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def color(color):
    colors.remove(color)
    text = random.choice(colors)
    label = tkinter.Label(window, text=color, fg = text, highlightthickness = 20)
    label.config(font=("Calibri", 44))
    buttonT = tkinter.Button(window, text=text)
    buttonF = tkinter.Button(window, text=color)
    colors.append(color)
    label.pack()
    buttonT.pack()
    buttonF.pack()

os.chdir(os.path.dirname(os.path.abspath(__file__)))

window.title('Color Game')
window.geometry('250x250')
instructions = tkinter.Label(window, text = 'Select word, not color!')
instructions.pack()
window.iconbitmap('icon.ico')
color(random.choice(colors))

window.mainloop()

It produces this window: 它产生以下窗口: 在此处输入图片说明

How can I check which button the user clicks in order to determine whether his answer is correct? 如何检查用户单击哪个按钮以确定他的答案是否正确? Can you please specify how I should implement your answer in my code? 您能否指定我应该如何在代码中实现答案? How does it work? 它是如何工作的?

Thanks in advance. 提前致谢。

  • Stavros 斯塔夫罗斯

You can use lambda functions for this. 您可以为此使用lambda函数。

def populateMethod(self, method):
    print "method:", method

for method in ["Red","Green","Blue"]:
    button = Button(window, text=method, 
        command=lambda m=method: self.populateMethod(m))

UPDATE: 更新:

I have modified your code and added the lambda function. 我已经修改了您的代码并添加了lambda函数。 Check and let me know if it works fine. 检查并让我知道它是否可以正常工作。

import tkinter, os, random

colors = ['Green', 'Red', 'Blue','Yellow', 'Pink', 'Orange', 'Purple', 'Grey', 'Brown', 'Black']

window = tkinter.Tk()
os.chdir(os.path.dirname(os.path.abspath(__file__)))


def populateMethod(method):
    print ("method:", method)

def color(color):
    colors.remove(color)
    text = random.choice(colors)
    label = tkinter.Label(window, text=color, fg = text, highlightthickness = 20)
    label.config(font=("Calibri", 44))
    buttonT = tkinter.Button(window, text=text,command=lambda m=text: populateMethod(m))
    buttonF = tkinter.Button(window, text=color,command=lambda m=color: populateMethod(m))
    colors.append(color)
    label.pack()
    buttonT.pack()
    buttonF.pack()

os.chdir(os.path.dirname(os.path.abspath(__file__)))

window.title('Color Game')
window.geometry('250x250')
instructions = tkinter.Label(window, text = 'Select word, not color!')
instructions.pack()
# window.iconbitmap('icon.ico')
color(random.choice(colors))

window.mainloop()

You should use command parameter on button, like this: 您应该在按钮上使用命令参数,如下所示:

def callback():
    print('button t clicked')

buttonT = tkinter.Button(window, text=text, command=callback)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM