python-3.x 当我运行程序时,文本打印两次,我如何解决这个问题?

rkkpypqq  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(127)

当我运行程序时,文本打印两次,我如何解决这个问题?选项根据问题列表中的数字显示。文本显示两次。

from tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width=750, height=750)
canvas.pack()

def startEasy():
    questions = [1, 2]
    random.shuffle(questions)
    for x in range(len(questions)):
        if questions[x] == 1:
            txt = "How many legs does an octopus have?"
            options=['a. 7', 'b. 4', 'c. 8']
            execute(txt, options)
        elif questions[x] == 2:
            txt = "How many legs does an octopus have z?"
            options=['a. 8', 'b. 5', 'c. 8']
            execute(txt, options)
      
def execute(text1, options):
    canvas.create_text(375, 200, text=text1, font=('Courier', 14))
    aBtn = Button(tk, text=options[0],font=("Courier", 30), width=15, height=1, bg='white')
    bBtn = Button(tk, text=options[1],font=("Courier", 30), width=15, height=1, bg='white')
    cBtn = Button(tk, text=options[2],font=("Courier", 30), width=15, height=1, bg='white')
    aBtn.place(x=187.5, y=225)
    bBtn.place(x=187.5, y=300)
    cBtn.place(x=187.5, y=375)

    
startEasy()
9jyewag0

9jyewag01#

如果不起作用,试试这个。
注解掉startEasy()函数中的text1 = text1 = text2[x]。将text1替换为txt = text2[x]

if questions1[x] == 1:
    txt = text2[x]
    options=['a. 7', 'b. 4', 'c. 8']
    correctAnswer='c'
    #execute(text1, options, correctAnswer)
elif questions1[x] == 2:
    txt = text2[x]
    options=['a. 8', 'b. 5', 'c. 8']
    correctAnswer='a'
execute(txt, options, correctAnswer)

相关问题