如何使 pygame 琐事问题出现在条目小部件中 [英] How do I make a pygame trivia question appear in the entry widget

查看:66
本文介绍了如何使 pygame 琐事问题出现在条目小部件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pygame 创建一个琐事游戏.第一个琐事问题出现在条目小部件上.但是,我希望在正确回答第一个问题后,第二个问题出现在条目小部件中.此外,如果玩家在任何时候选择了错误的答案,我希望游戏退出/结束.

I'm trying to create a trivia game using pygame. The first trivia question appears on the entry widget. However, I want the second question to appear in the entry widget once the first question has been answered correctly. Also If at any point the player selects a wrong answer I want the game to exit/end.

为冗长的代码行道歉.我很想把问题和答案放在一个文本文件中,而不是直接在 python 中编码,但我显然不知道该怎么做.

Apologies for the long line of code. I would have loved to put the question and answer in a text file instead of coding them directly inside python, but I obviously don't know how to do go about it.

from tkinter import *
import tkinter as tk
import pygame as py
from PIL import ImageTk, Image
import time, random, sys
from pygame.locals import *

py.init()
py.mixer.init()
root=Tk()
root.title("Who Wants To Be A Millionaire")
root.geometry=('1352x625+0+0')
root.configure(background ='black')
abc= Frame(root, bg = 'black')
abc.grid()

abc1= Frame(root, bg = 'black', bd=20, width=900, height=600)
abc1.grid(row=0, column=0)
abc2= Frame(root, bg = 'black', bd=20, width=452, height=600)
abc2.grid(row=0, column=1)

abc1a= Frame(abc1, bg = 'black', bd=20, width=900, padx=110, height=200)
abc1a.grid(row=0, column=0)
abc1b= Frame(abc1, bg = 'black', bd=20, width=900, height=200)
abc1b.grid(row=1, column=0)
abc1c= Frame(abc1, bg = 'black', bd=20, width=900, height=200)
abc1c.grid(row=2, column=0)

#===========================================Trivia Questions==============================================

question1 = StringVar()
question2 = StringVar()
question3 = StringVar()
question4 = StringVar()
question5 = StringVar()
question6 = StringVar()
question7 = StringVar()
question8 = StringVar()
question9 = StringVar()
question10 = StringVar()
question11 = StringVar()
question12 = StringVar()
question13 = StringVar()
question14 = StringVar()
question15 = StringVar()


choice1 = StringVar()
choice2 = StringVar()
choice3 = StringVar()
choice4 = StringVar()


question1.set("What's the capital of Nigeria")
choice1.set('Mongoria')
choice2.set('Cairo')
choice3.set('Accra')
choice4.set('Abuja')


question2.set(" Which one of the color can be found in US National Flag")
choice1.set("Green")
choice2.set('Red')
choice3.set('Orange')
choice4.set('Yelow')

question3.set("What is 5000 x 110")
choice1.set("55,110")
choice2.set('56,100')
choice3.set('50,000')
choice4.set('55,000')

question4.set("Whitney Houston died in which year")
choice1.set("2013")
choice2.set('2010')
choice3.set('2012')
choice4.set('2011')

question5.set("How many countries are there in Africa")
choice1.set("32")
choice2.set('43')
choice3.set('45')
choice4.set('30')


#===========================================Text-Labels-Button==============================================
textQues= Entry(abc1c, font= ('arial',18, 'bold'), bg='black',fg='white', bd= 2, width=50, justify= CENTER,textvariable= question1)
textQues.grid(row=0, column=0, columnspan=10, pady=4)

label_option1= Label(abc1c, font= ('arial',14, 'bold'), text='A: ', bg='black',fg='white', bd= 5, justify = CENTER)
label_option1.grid(row=2, column=0, pady=4, sticky=W)
text_option1= Button(abc1c, font= ('arial',14, 'bold'), bg='blue',fg='white', bd= 5, width=17, height= 2, justify = CENTER, textvariable=choice1)
text_option1.grid(row=2, column=1, pady=4)


label_option2= Label(abc1c, font= ('arial',14, 'bold'), text='B: ', bg='black',fg='white', bd= 5, justify = CENTER)
label_option2.grid(row=2, column=2, pady=3, sticky=W)
text_option2= Button(abc1c, font= ('arial',14, 'bold'), bg='blue',fg='white', bd= 5, width=17, height= 2, justify = CENTER, textvariable=choice2)
text_option2.grid(row=2, column=3, pady=4)


label_option3= Label(abc1c, font= ('arial',14, 'bold'), text='C:', bg='black',fg='white', bd= 5, justify = CENTER)
label_option3.grid(row=3, column=0, pady=4, sticky=W)
text_option3= Button(abc1c, font= ('arial',14, 'bold'), bg='blue',fg='white', bd= 5, width=17, height= 2, justify = CENTER, textvariable=choice3)
text_option3.grid(row=3, column=1, pady=4)


label_option4= Label(abc1c, font= ('arial',14, 'bold'), text='D: ', bg='black',fg='white', bd= 5, justify = CENTER)
label_option4.grid(row=3, column=2, pady=4, sticky=W)
text_option4= Button(abc1c, font= ('arial',14, 'bold'), bg='blue',fg='white', bd= 5, width=17, height= 2, justify = CENTER, textvariable= choice4)
text_option4.grid(row=3, column=3, pady=4)


raytxt=Label(abc2,font=('Cambri', 12, 'italic'), text='Built by: Raymond French ', bg='black', fg='white', bd= 5, justify= CENTER)
raytxt.grid(row=1, column=0)


root.mainloop()

推荐答案

您应该将问题、选择和正确答案保留在列表中.稍后您可以从文件中读取数据

You should keep quetion, choices and correct answer on list. Later you can read data from file

这种方式不必创建这么多小部件.您可以创建没有文本的小部件,并使用在小部件中放置/替换问题和选项的功能.

This way don't have to create so many widgets. You can create widgets without text and use function which put/replace question and choices in widgets.

当您单击答案时,它应该运行检查所选答案是否正确的功能.然后你可以再次运行替换小部件中文本的函数.

When you click answer it should run function which check if selected answer is correct. And then you can run again function which replace text in widget.

我只使用重要元素创建了最小的工作示例.现在代码更短了.

I create minimal working example only with important elements. And now code is much shorter.

import tkinter as tk


def set_question(number):
    global correct_answer

    row = all_questions[number]

    question.set(row[0])

    choice_a.set(row[1])
    choice_b.set(row[2])
    choice_c.set(row[3])
    choice_d.set(row[4])

    correct_answer = row[5]


def selected(selected_number):
    global current_question

    if selected_number != correct_answer:
        print('Game Over')
        root.destroy()
    else:
        current_question += 1
        if current_question >= len(all_questions):
            current_question = 0
        set_question(current_question)


all_questions = [
    #[question, choice_a, choice_b, choice_c, choice_d, correct_answer],
    ["What's the capital of Nigeria", 'Mongoria', 'Cairo', 'Accra', 'Abuja', 4],
    ["Which one of the color can be found in US National Flag", "Green", 'Red', 'Orange', 'Yelow', 2],
    ["What year is today", "2017", '2018', '2019', '2020', 3],
]

current_question = 0

root = tk.Tk()

top_frame = tk.Frame(root)
top_frame.grid(row=2, column=0)

question = tk.StringVar()
choice_a = tk.StringVar()
choice_b = tk.StringVar()
choice_c = tk.StringVar()
choice_d = tk.StringVar()

# create Widgets for question and choices but without text
textQues = tk.Entry(top_frame, textvariable=question)
textQues.grid(row=0, column=0, columnspan=10)

label_option1 = tk.Label(top_frame, text='A:')
label_option1.grid(row=2, column=0)
text_option1 = tk.Button(top_frame, textvariable=choice_a, command=lambda:selected(1))
text_option1.grid(row=2, column=1)

label_option2 = tk.Label(top_frame, text='B:')
label_option2.grid(row=2, column=2)
text_option2 = tk.Button(top_frame, textvariable=choice_b, command=lambda:selected(2))
text_option2.grid(row=2, column=3)

label_option3 = tk.Label(top_frame, text='C:')
label_option3.grid(row=3, column=0)
text_option3 = tk.Button(top_frame, textvariable=choice_c, command=lambda:selected(3))
text_option3.grid(row=3, column=1)

label_option4 = tk.Label(top_frame, text='D:')
label_option4.grid(row=3, column=2)
text_option4 = tk.Button(top_frame, textvariable=choice_d, command=lambda:selected(4))
text_option4.grid(row=3, column=3)

# put first question in Widgets
set_question(current_question)

root.mainloop()

顺便说一句:并使用变量的名称,这意味着什么 - 即.top_frame 而不是 abc

BTW: and use variable's names which means something - ie. top_frame instead of abc

这篇关于如何使 pygame 琐事问题出现在条目小部件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆