如何在条目小部件中显示pygame trivia问题

问题描述 投票:0回答:1

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

为长行代码道歉。我本来喜欢把问题和答案放在一个文本文件中而不是直接在python中编码,但我显然不知道怎么做。

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()
pygame python-3.7 tkinter-entry tkinter-layout
1个回答
0
投票

您应该在列表中保留排队,选择和正确答案。稍后您可以从文件中读取数据

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

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

我只使用重要元素创建最小的工作示例。现在代码要短得多。

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()

BTW:并使用变量的名称,这意味着什么 - 即。 top_frame而不是abc

© www.soinside.com 2019 - 2024. All rights reserved.