'int'对象没有属性'grid'

时间:2019-05-30 15:20:37

标签: python tkinter tkinter-canvas

我正在使用tkinter在python中进行问答游戏。我使用create_text()创建文本,但不断收到此错误canvas_text.grid(row = 0) AttributeError:“ int”对象没有属性“ grid”。请帮我! 我是tk(和Python)的菜鸟。

from tkinter import * 
from time import sleep
import random as r
from PIL import ImageTk,Image
class Question:
    def __init__(self, question, answers, correctLetter):
        self.question = question
        self.answers = answers
        self.correctLetter = correctLetter

    def check(self, letter, view):
        global right
        if(letter == self.correctLetter):
            label = Label(view, text="Right!")
            right += 1
        else:
            label = Label(view, text="Wrong!")
        label.grid()
        view.after(700, lambda *args: self.unpackView(view))

    def getView(self, window):
        view = Canvas(window)
        canvas_text=view.create_text((200,175),text=self.question, fill="#652828")
    canvas_text.grid(row=0)
    b1=Button(view, text=self.answers[0], command=lambda *args: self.check("A", view))
    b1.grid(row=1,column=0)
    b2=Button(view, text=self.answers[1], command=lambda *args: self.check("B", view))
    b2.grid(row=1,column=1)
    b3=Button(view, text=self.answers[2], command=lambda *args: self.check("C", view))
    b3.grid(row=2,column=0)
    b4=Button(view, text=self.answers[3], command=lambda *args: self.check("D", view))
    b4.grid(row=2,column=1)
    return view

    def unpackView(self, view):
        view.grid_forget()
        askQuestion()

    def askQuestion():
    global questions, window, index, button, right, number_of_questions 
    if(len(questions) == index + 1):
        Label(window, text="Thank you for answering the questions. "+str(right) + " of " + str(number_of_questions) + " questions answered right").grid()
    return
    button.grid_forget()

index += 1
questions[index].getView(window).grid()
def resize_image(event):
new_width = event.width
new_height = event.height

image = image1.resize((new_width, new_height))
image2 = ImageTk.PhotoImage(image)

l.config(image=image2)
l.image = image2

questions = []
file = open("questions.txt", "r")
line = file.readline()
while(line != ""):
    questionString = line
    answers = []
    for i in range (4):
        answers.append(file.readline())

    correctLetter = file.readline()
    correctLetter = correctLetter[:-1]
    questions.append(Question(questionString, answers, correctLetter))
    line = file.readline()
file.close()
index = -1
right = 0
number_of_questions = len(questions)

window = Tk()
window.title('Welcome to Kaun Banega Crorepati:')
window.geometry('600x600')
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1) 

frame = Canvas(window, relief='raised', borderwidth=2)
frame.grid(column=0, row=0, sticky = "nsew")
frame.grid_rowconfigure(0, weight = 1)
frame.grid_columnconfigure(0, weight = 1)
frame.grid_propagate(False)

image1 = Image.open("Untitled.png")
image2 = ImageTk.PhotoImage(image1)

l = Label(frame, image=image2)
l.place(x=0, y=0, relwidth=1, relheight=1)
l.bind('<Configure>', resize_image)

button = Button(frame, text="Start", command=askQuestion)
button.place_configure(x=200,y=175)

window.mainloop()

这是我遇到的错误。

Tkinter回调中的异常

 Traceback (most recent call last):
    File "C:\Users\RENU\AppData\Local\Programs\Python\Python37- 
    32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
    File "C:\Users\RENU\Desktop\annual projext\tkinter.py", line 47, in 
    askQuestion
    questions[index].getView(window).grid()
    File "C:\Users\RENU\Desktop\annual projext\tkinter.py", line 24, in getView
    canvas_text.grid(row=0)
    AttributeError: 'int' object has no attribute 'grid'

我该怎么办?

0 个答案:

没有答案