python - 这可能是一个基本问题,但我需要配置一些东西但发生错误
问题描述
在开始之前,我在 windows 和 python 3 上。
当我尝试运行我的代码时出现此错误。
AttributeError:“int”对象没有属性“配置”
代码是这样的。我没有包括一些东西,包括我制作的文件(软编码)和 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 还很陌生,所以我需要帮助修复我的代码以消除该错误,如果有人可以添加如何将我的代码线程化到它不会干扰主循环会很棒,因为我以前从未线程过这样的东西。所以我需要两件事,第一件事是修复这个错误消息,然后如果有人可以帮我解决这个问题。我需要的主要事情是让它开始工作,然后如果有人可以帮助处理它,那将是非常有义务的。
解决方案
您没有显示足够的代码来重现您的错误,但是我可以根据您的问题告诉您,您正在尝试配置一个整数而不是最初看起来像按钮或标签的东西。
此行导致您的问题:
question = random.randint(1,30)
您正在将按钮/标签重新定义为整数,然后尝试对其进行配置。
尝试将此行更改question = random.randint(1,30)
为类似的question_int = random.randint(1,30)
内容,您应该可以克服这个变量冲突。
推荐阅读
- javascript - 在 d3.js v5 (timeScale) 中制作全宽 xAxis
- php - PHP未定义变量:数组
- arrays - 为什么 Join() 需要一个 1-dim Long 数组的双重转置?
- vue.js - Vue - 我应该在哪里声明元素变量以在任何生命周期或方法中使用?
- mysql - 检索具有最大值mysql的行
- c# - 是否可以在同一个 Viewbag 变量上传递多个值?
- corda - Corda 可调度状态导致流测试挂起
- javascript - 等待量角器与页面同步时出错:“angularJS 可测试性和 Angular 可测试性都未定义
- ubuntu - Caffe 编译错误 - 虚拟框中的 Ubuntu 18.04.4 Live Server
- python-3.x - 从点生成二维网格文件