【发布时间】:2019-04-02 16:12:50
【问题描述】:
在开始之前,我在 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 还很陌生,所以我需要帮助修复我的代码以消除该错误,如果有人可以添加如何将我的代码线程化到它不会干扰主循环会很棒,因为我以前从未线程过这样的东西。 所以我需要两件事,第一件事是修复这个错误消息,然后如果有人可以帮我解决这个问题。我需要做的主要事情是让它开始工作,然后如果有人可以帮助处理它,那将非常感激。
【问题讨论】:
-
除非您正在为您的
dialog2函数运行某种线程,否则您需要执行除使用while之外的其他操作。while循环将冻结主循环并导致问题。 -
我的整个代码,除了要导入的代码和主屏幕以及所有其他没有帮助的 if 语句
-
还请包括您的导入以及如何调用您的
dialog1和dialog2函数。 -
所以基本上除了我的 if 语句之外的所有内容。好的
-
嗯,这就是我们目前所处的位置。如果我在下面的回答没有解决您的问题,那么您的问题比看起来要大。