首页 > 解决方案 > TKINTER 问答游戏

问题描述

我是编程和 Python 的初学者。我正在尝试使用 tkinter 构建一个问答游戏,我在其中调用一个 csv 文件,该文件在 python 文件中具有 6 列(QUESTION、ANSWERA、ANSWERB、ANSWERC、ANSWERD、CORRECTANSWER)。我有一个输入框(用于问题)和 4 个按钮(用于答案)。

我的问题:如何通过按钮中的命令对为 csv 文件创建的列表进行交互?

当我单击按钮时,无论答案是否正确,我希望它更改按钮处的文本并从列表中的下一行获取值。

我的代码:

from tkinter import*
import pygame
import sys

pygame.init()
root = Tk()
root.title("ΜΑΘΕ ΠΑΙΖΟΝΤΑΣ")
root.geometry('1352x652+0+0')
root.configure(background = 'blue')

#=================CONNECT TO DATABASE csv=========================
import csv
import unicodedata

with open('questiondb.csv',newline='',encoding="utf8")as csvfile:
    csvreader=csv.reader(csvfile,delimiter=',',quotechar='|')
    rows=list(csvreader)
    for i in range(1,3):
        print(rows[i])
#=================functions=============
def correct_answer_funcA():
    if answerA==correctanswer:
        print ("right!!!")
    else:
        print("wrong!!")
def correct_answer_funcB():
    if answerB==correctanswer:
        print ("right!!!")
    else:
        print("wrong!!")
def correct_answer_funcC():
    if answerC==correctanswer:
        print ("right!!!")
    else:
        print("wrong!!")
def correct_answer_funcD():
    if answerD==correctanswer:
        print ("right!!!")
    else:
        print("wrong!!")

#===============TEXT,LABELS,BUTTON=======================
i=1
questions=rows[i][1]
answerA=rows[i][2]
answerB=rows[i][3]
answerC=rows[i][4]
answerD=rows[i][5]
correctanswer=rows[i][6]


txtQuestion = Entry(ABC1c, font=('arial', 18,'bold'),bg='red', fg='white', bd=1, width=44,justify=CENTER)
txtQuestion.grid(row=0, column=0, columnspan=4, pady=4)
txtQuestion.insert(INSERT,questions)

lblQuestA = Label(ABC1c, font=('arial', 14,'bold'),text="A: ",bg='black', fg='white', bd=1, justify=CENTER)
lblQuestA.grid(row=1, column=0, pady=4, sticky=W)
txtQuestion1 = Button(ABC1c, font=('arial', 14,'bold'),bg='blue', fg='white', bd=1, width=17, height=2,justify=CENTER, text=answerA, command=correct_answer_funcA)
txtQuestion1.grid(row=1, column=1, pady=4)

lblQuestB = Label(ABC1c, font=('arial', 14,'bold'),text="B: ",bg='black', fg='white', bd=1,justify=LEFT)
lblQuestB.grid(row=1, column=2, pady=4, sticky=W)
txtQuestion2 = Button(ABC1c, font=('arial', 14,'bold'),bg='blue', fg='white', bd=1, width=17, height=2,justify=CENTER, text = answerB, command=correct_answer_funcB)
txtQuestion2.grid(row=1, column=3, pady=4)

lblQuestC = Label(ABC1c, font=('arial', 14,'bold'),text="C: ",bg='black', fg='white', bd=1, justify=CENTER)
lblQuestC.grid(row=2, column=0, pady=4, sticky=W)
txtQuestion3 = Button(ABC1c, font=('arial', 14,'bold'),bg='blue', fg='white', bd=1, width=17, height=2,justify=CENTER, text = answerC, command=correct_answer_funcC)
txtQuestion3.grid(row=2, column=1, pady=4)

lblQuestD = Label(ABC1c, font=('arial', 14,'bold'),text="B: ",bg='black', fg='white', bd=1,justify=LEFT)
lblQuestD.grid(row=2, column=2, pady=4, sticky=W)
txtQuestion4 = Button(ABC1c, font=('arial', 14,'bold'),bg='blue', fg='white', bd=1, width=17, height=2,justify=CENTER, text = answerD, command=correct_answer_funcD)
txtQuestion4.grid(row=2, column=3, pady=4)

标签: pythontkinter

解决方案


推荐阅读