python中tkinter模块_如何在python模块tkinter中处理单击按钮事件

1586010002-jmsa.png

I am trying to make a question game in python using tkinter. I am struggling to define a function that can check the answer that the player clicked on and add to a score that is then printed out below the answers.

The outcome of the code whenever I click is that the score is 0 and the question and answers don't change. Also, if I click repeatedly, it prints 0 as a label below each other.

I only included all of my code so that if someone wanted to test the code out for themselves, it wouldn't throw up any errors.

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

解决方案

Starting at the top, let's change your check_answer definition to not create a new label every time:

def check_answer(answer):

if answer == answers[s][0] or answer == answers[s][1]:

global score

score += 1

lbl["text"] = score

Next, we need one small change in your for loop: we want to send answer, not 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()

Lastly, we'll add that label that we removed earlier down to the bottom where you had it initially. You can change the location of this by packing it sooner in the code for aesthetics. Your code still doesn't cycle to a new question once one is answered (correctly or otherwise), but at least now you can see when the user answers correctly.

Hope this helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值