在开始之前,我在Windows和python 3上。
尝试运行代码时出现此错误。
AttributeError:“ int”对象没有属性“ 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循环的要点。然后它给了我进入对象的错误,并且对于gui和tkinter来说我还是很陌生,所以如果有人可以添加如何将我的代码线程化,所以我需要帮助修复我的代码以摆脱该错误。干扰了主循环,因为我以前从未线程过类似的东西,这会很棒。 因此,我需要做两件事,第一件事是修复此错误消息,然后如果有人可以帮助我对此进行处理。我需要的主要是使它开始工作,然后如果有人可以帮助对其进行线程化,那将非常有必要。
答案 0 :(得分:2)
您没有显示足够的代码来重现错误,但是我可以根据您的问题告诉您,您正在尝试配置一个整数,而不是最初看起来像按钮或标签的整数。
此行引起您的问题:
question = random.randint(1,30)
您正在将按钮/标签重新定义为整数,然后尝试对其进行配置。
尝试将此行question = random.randint(1,30)
更改为question_int = random.randint(1,30)
之类的内容,您应该克服此变量冲突。
答案 1 :(得分:0)
您的变量“ question”是一个整数,您可能在代码中的更高处做了错误的事情。
编辑:
question = random.randint(1,30)
问题是整数类型,因此您不能使用.configuration()方法。
如果您想选择一个随机问题,可以声明一个列表并使用random.choice
方法
questions_list = [
"my_first_question",
"my_second_question",
"ect..."
]
random.choice(questions_list)