特金特问答游戏

2024-09-28 22:55:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程和Python的初学者。我试图用tkinter构建一个问答游戏,在这个游戏中,我调用了一个csv文件,该文件在python文件中有6列(问题、回答、回答、回答、回答)。我有一个输入框(用于问题)和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)

Tags: columnbdgridrowsrowbgprintwhite