我只想让我的代码首先绑定到红色 并使红色的处理程序在红色结束后重新绑定为黄色。林不知道如何做到这一点。有谁知道这是怎么做的。因为在我的代码中,如果您运行它。它只运行黄色函数而不是红色函数,因此我确定这是因为我覆盖了绑定函数。
from tkinter import *
import random
root = Tk()
root.geometry("300x300")
score = 0
lb1 = Listbox(root)
e = Entry(root)
def binder1():
root.bind('<Return>', red)
def red(event):
print(e.get())
if e.get() == "red":
lb1.insert("end", "Correct")
binder1
global score
score += 1
mb4['text'] = str("Score ") + str(score)
redcolour = Label(root, text = "What colour is this boi!", fg = "Red")
def yellow(event):
print(e.get())
if e.get() == "yellow":
lb1.insert("end", "Correct")
global score
score += 1
mb4['text'] = str("Score ") + str(score)
root.bind('<Return>', yellow)
yellowcolour = Label(root, text = "What colour is this boi!", fg = "Yellow")
mb4= Menubutton(root, text = str("Score: ") + str(score))
mb4.menu = Menu(mb4)
e.focus_set()
e.pack()
yellowcolour.pack()
redcolour.pack()
lb1.pack()
mb4.pack()
root.mainloop()
答案 0 :(得分:1)
对于我来说不清楚,为什么你要先绑定红色。
我将使其更加动态,并让用户做出决定。
像这样:
from tkinter import *
def check_input(event):
global score
my_input = e.get()
if my_input == "yellow":
lb1.insert("end", "Correct yellow")
score += 1
mb4['text'] = str("Score ") + str(score)
elif my_input == "red":
lb1.insert("end", "Correct red")
score += 1
mb4['text'] = str("Score ") + str(score)
else:
return None
if __name__ == "__main__":
root = Tk()
root.geometry("300x300")
root.bind('<Return>', check_input)
score = 0
lb1 = Listbox(root)
e = Entry(root)
redcolour = Label(root, text = "What colour is this boi!", fg = "Red")
yellowcolour = Label(root, text = "What colour is this boi!", fg = "Yellow")
mb4= Menubutton(root, text = str("Score: ") + str(score))
e.focus_set()
e.pack()
yellowcolour.pack()
redcolour.pack()
lb1.pack()
mb4.pack()
root.mainloop()