所以我试图做这个简单的选择题测验,我想使其面向对象。我创建了一个可以围绕一个问题建模的类,但是我想在第一个问题得到回答时 我想开始第二个问题,但是我已经尝试过对类var进行更改,但是没有任何作用
我的代码:
import tkinter
from tkinter import *
root = Tk()
canvas = Canvas(root, width=500, height=500)
canvas.pack()
class Window:
def __init__(self, root, canvas, question, a, b, c, correct):
self.root = root
self.canvas = canvas
self.question = question
self.answerA = a
self.answerB = b
self.answerC = c
self.answer = correct
self.final = False
if self.answer == 1:
self.canvas.tag_bind('A', '<Button 1>', self.correct)
self.canvas.tag_bind('B', '<Button 1>', self.wrong)
self.canvas.tag_bind('C', '<Button 1>', self.wrong)
elif self.answer == 2:
self.canvas.tag_bind('A', '<Button 1>', self.wrong)
self.canvas.tag_bind('B', '<Button 1>', self.correct)
self.canvas.tag_bind('C', '<Button 1>', self.wrong)
elif self.answer == 3:
self.canvas.tag_bind('A', '<Button 1>', self.wrong)
self.canvas.tag_bind('B', '<Button 1>', self.wrong)
self.canvas.tag_bind('C', '<Button 1>', self.correct)
self.canvas.tag_bind('Again', '<Button 1>', self.redirect)
self.canvas.tag_bind('Correct', '<Button 1>', self.redirect)
def draw(self):
self.canvas.delete(ALL)
self.canvas.create_text(250, 125, text=self.question, font=('Helvetica', 20))
self.canvas.create_rectangle(100, 250, 150, 300, fill='Black', activefill='Orange', tags='A')
self.canvas.create_rectangle(225, 250, 275, 300, fill='Black', activefill='Orange', tags='B')
self.canvas.create_rectangle(350, 250, 400, 300, fill='Black', activefill='Orange', tags='C')
self.canvas.create_text(125, 350, text=self.answerA, font=('Helvetica', 15), width=100)
self.canvas.create_text(250, 350, text=self.answerB, font=('Helvetica', 15), width=100)
self.canvas.create_text(375, 350, text=self.answerC, font=('Helvetica', 15), width=100)
def correct(self, event):
self.canvas.delete(ALL)
self.canvas.create_text(250, 125, text='Correct', font=('Helvetica', 20))
self.canvas.create_rectangle(125, 250, 375, 400, fill='Black', activefill='Orange', tags='Correct')
self.right = True
self.wrongg = False
def wrong(self, event):
self.canvas.delete(ALL)
self.canvas.create_text(250, 125, text='Wrong', font=('Helvetica', 20))
self.canvas.create_rectangle(125, 250, 375, 400, fill='Black', activefill='Orange', tags='Again')
self.wrongg = True
self.right = False
def redirect(self, event):
print('Redirecting...')
if self.wrongg == True:
print('Drawing...')
self.draw()
elif self.correct == True:
self.final = True
questionOne = Window(root, canvas, 'test', 'a', 'b', 'c', 3)
questionOne.draw()
root.mainloop()