这可能是一个基本的问题,但我需要配置的东西,但发生了错误

2024-06-18 04:45:12 发布

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

在开始之前,我使用的是windows和python3。你知道吗

我在尝试运行代码时遇到了这个错误。你知道吗

AttributeError: 'int' object has no attribute 'configure'

代码是这样的。有一些东西我没有包括在内,包括我制作的文件(软编码)和if语句(因为它们都是复制和粘贴的,因为它们彼此相似)。你知道吗

import tkinter
from tkinter import *
import tkinter.messagebox as box
import time
import random
import hashlib
import winsound
import webbrowser
import os

#I'll hardcode one file for the test
file.open("Song name12","w")
file.write("Artist: Ricky martin\n Song initals: L L V L")
file.close()
file.open("Song name12","r")
a12 = file.read()

def dialog1():
    global answer1
    global question
    global score
    score=0
    global counter
    counter=0
    global counter1
    counter1=3
    username=entry1.get()
    password = entry2.get()
    if (username == 'a' and  password == 'b'):
        box.showinfo('info','You may now enter the Music quiz')
        Quiz = Frame(window)
        Quiz.pack()
    def dialog2():
        global score
        score = 0
        global counter
        counter = 0
        condition = True
        final_answer = answer.get()
        answer.delete(0, END)
        if final_answer == ans2:
            counter = counter+1
            print("Correct")
            old = question2
            while old == question2:
                question = random.randint(1,30)

                if question2 == 12:
                    question.configure(text=a12)
                    question.pack()

    if question2 == 12:
        Text = Label(Quiz,text = 'What is this song?')
        Text.pack()
        question = Label(Quiz,text = a12)
        question.pack()
        answer = Entry(Quiz)
        answer.pack()
        Button1 = Button(Quiz, text='Check answer',command=dialog2)
        Button1.pack()
        ans2 = "Livin la vida loca"
window = Tk()
window.title('Music quiz')
window.geometry("300x125")
window.wm_iconbitmap('Favicon.ico')
loginframe = Frame(window)  #create an empty frame for login
loginframe.pack()  #put the empty frame into the window


Label1 = Label(loginframe,text = 'Username:')
Label1.pack()

entry1 = Entry(loginframe)
entry1.pack()

Label2 = Label(loginframe,text = 'Password: ')
Label2.pack()

entry2 = Entry(loginframe)
entry2.pack()

donebttn = Button(loginframe, text='Done',command=dialog1)#create a button to continue
donebttn.pack()  #display that button


mainloop()

基本上,这段代码一直工作到while循环。然后它给了我一个into对象的错误,我对gui和tkinter还是相当陌生的,所以我需要帮助修复我的代码来消除这个错误,如果有人可以添加如何将我的代码线程化,这样它就不会干扰主循环,这将是非常好的,因为我以前从来没有线程化过这样的东西。 所以有两件事我需要,第一是修复这个错误消息,然后如果有人可以帮我线程。我需要的主要事情是这个开始工作,然后如果有人可以帮助线程,这将是非常感谢。你知道吗


Tags: 代码textanswerimportiftkinter错误counter
2条回答

您的变量“question”是一个整数,您可能在代码的更高层做了一些错误的事情。你知道吗

编辑:

question = random.randint(1,30)

question是整数类型,所以不能使用.configuration()方法。 如果您想随机选择一个问题,可以声明一个列表并使用random.choice方法

questions_list = [
    "my_first_question",
    "my_second_question",
    "ect..."
    ]

random.choice(questions_list)

你没有显示足够的代码来重现你的错误,但我可以告诉你,根据你的问题,你试图配置一个整数,而不是什么看起来像原来是一个按钮或标签。你知道吗

这条线导致了您的问题:

question = random.randint(1,30)

您正在将按钮/标签重新定义为整数,然后尝试对其进行配置。你知道吗

试着把这行question = random.randint(1,30)改成类似question_int = random.randint(1,30)的东西,你就应该克服这个变量冲突了。你知道吗

相关问题 更多 >