【问题标题】:How to handle a click button event in python module tkinter如何处理python模块tkinter中的单击按钮事件
【发布时间】:2018-06-23 00:43:01
【问题描述】:

我正在尝试使用 tkinter 在 python 中制作一个问题游戏。我正在努力定义一个函数,该函数可以检查玩家点击的答案并添加到分数,然后在答案下方打印出来。

每当我点击代码的结果是分数为 0 并且问题和答案没有改变。另外,如果我反复单击,它会在彼此下方打印 0 作为标签。

我只包含了我所有的代码,这样如果有人想自己测试代码,就不会抛出任何错误。

import tkinter
import random
from random import shuffle

score = 0

def check_answer(answer):
    if answer == answers[s][0] or answer == answers[s][1]:
        global score
        score += 1
    lbl = tkinter.Label(window, text=score)
    lbl.pack()


#This sets up a window and names it "The Hunt" but doesn't generate the window
window = tkinter.Tk()
window.title("The Hunt!")

#This sets the background colour
window.configure(background="#1C3F95")

#This generates labels and buttons that are the same or similar colour to the background
welcome = tkinter.Label(window, text="Welcome to the Hunt!", bg="#1C3F95")
begin = tkinter.Button(window, text="Click here to begin", bg="#1C7C95")


#This is my code for the question generation. As I made that for a pygame window, I obviously had to change it slightly

questions = ["What species of bird is also a nickname for New Zealand?", "Which Twins can you play as in Assassin's Creed Syndicate?",
                 "Which year was 'Killing In The Name' Christmas Number one?"]

answers = [["kiwi", "Kiwi", "Falcon", "Sparrow", "Crow"], ["frye", "Frye", "Bank", "Green", "Bundy"], ["2009", "2009",
                                                                                                       "1999", "1993",
                                                                                                       "2004"]]
#I had to do it in two separate lists as it's easier to work with later on
# Also I made the correct answers non-case sensitive to make it easier to test.

r = len(questions)
score = 0
s = random.randrange(0, r, 1)
#This generates a random number within the range of how many questions there are
# and then prints out that question

#This generates a label that displays the randomly generated question
question = tkinter.Label(window, text=questions[s])


list = answers[s]
output = []

for i in range(1, 5):
    output.append(answers[s][i])
shuffle(output)


# this takes the answers that correspond with the randomly generated question and shuffles the answers
# I did this as otherwise, the answer would always be the first answer to appear and the player could exploit this

#This code is what displays the labels and buttons on the window. It lets the computer decide where the best place for
#each component is
welcome.pack()
begin.pack()
question.pack()

for i in output:
    answer = tkinter.Button(window, text=i, command=lambda answer = i: check_answer(i))
    answer.pack()


#I had decided to pack the answers like this as it was easier than typing out each element of the list and also
#more efficent
window.mainloop()
#this is the code that actually generates the window

【问题讨论】:

    标签: python button tkinter label


    【解决方案1】:

    从顶部开始,让我们将您的 check_answer 定义更改为不每次都创建新标签:

    def check_answer(answer):
        if answer == answers[s][0] or answer == answers[s][1]:
            global score
            score += 1
            lbl["text"] = score
    

    接下来,我们需要在你的 for 循环中做一个小改动:我们要发送答案,而不是 i:

    for i in output:
        answer = tkinter.Button(window, text=i, command=lambda answer = i: check_answer(answer))
        answer.pack()
    
    lbl = tkinter.Label(window, text=score)
    lbl.pack()
    

    最后,我们会将之前移除的标签添加到您最初拥有它的底部。您可以通过将其更快地打包到代码中来更改它的位置以实现美观。一旦回答(正确或不正确),您的代码仍然不会循环到新问题,但至少现在您可以看到用户何时正确回答。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      首先,你在 lambda 回调中犯了一个小错误:

      command=lambda answer=i: check_answer(answer)
      

      应该是的。

      然后对于许多标签,创建一个标签并更改文本:

      def check_answer(answer):
          print(answer)
          if answer == answers[s][0] or answer == answers[s][1]:
              global score
              score += 1
          lbl.configure(text=str(score))
      

      (很多代码我没有复制)

      for i in output:
          answer = tkinter.Button(window, text=i, command=lambda answer=i: check_answer(answer))
          answer.pack()
      
      lbl = tkinter.Label(window, text=score)
      lbl.pack()
      

      【讨论】:

        猜你喜欢
        • 2011-10-16
        • 1970-01-01
        • 1970-01-01
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-12
        相关资源
        最近更新 更多