Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter错误对象没有属性_Python_Tkinter_Attributeerror - Fatal编程技术网

Python Tkinter错误对象没有属性

Python Tkinter错误对象没有属性,python,tkinter,attributeerror,Python,Tkinter,Attributeerror,所以我正在制作一个类似于街机游戏的程序。我希望在单击框架后,标签UESS出现在顶层窗口中,但它会给我以下错误: AttributeError:“窗口”对象没有属性“窗口” 代码如下: from tkinter import * from tkinter import font import time class Window(Frame): def __init__(self, master): Frame.__init__(self, master)

所以我正在制作一个类似于街机游戏的程序。我希望在单击框架后,标签UESS出现在顶层窗口中,但它会给我以下错误:

AttributeError:“窗口”对象没有属性“窗口”

代码如下:

from tkinter import *
from tkinter import font
import time

class Window(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)
        self.master = master

        master.title("Arcade Games")
        master.geometry("800x600+560+240")

        b = Button(self, text="Guess the number", command=self.new_window)
        b.pack(side="top")
        self.customFont = font.Font(master, font="Heraldica", size=12)

        self.guess_number()

    def new_window(self):

        id = "Welcome to the 'Guess your number' game!\nAll you need to do is follow the steps\nand I will guess your number!\n\nClick anywhere to start!"
        self.window = Toplevel(self.master)
        frame = Frame(self.window)
        frame.bind("<Button-1>", self.guess_number)
        frame.pack()
        self.window.title("Guess the number")
        self.window.geometry("400x300+710+390")
        label = Label(self.window, text=id, font=self.customFont)
        label.pack(side="top", fill="both", padx=20, pady=20)

    def guess_number(self):


        labelGuess = Label(self.window, text="Pick a number between 1 and 10", font=self.customFont)
        time.sleep(2)
        labelGuess.pack(fill=BOTH, padx=20, pady=20)

if __name__ == "__main__":
    root = Tk()
    view = Window(root)
    view.pack(side="top", fill="both", expand=True)
    root.mainloop()
从tkinter导入*
从tkinter导入字体
导入时间
类窗口(框架):
定义初始(自我,主):
帧。\uuuu初始化(自,主)
self.master=master
大师头衔(“街机游戏”)
主几何(“800x600+560+240”)
b=按钮(self,text=“猜数字”,command=self.new\u窗口)
b、 包装(侧面=“顶部”)
self.customFont=font.font(master,font=“Heraldica”,size=12)
self.guess_number()
def新窗口(自):
id=“欢迎参加“猜你的号码”游戏!\n你需要做的只是按照步骤进行操作,\n我会猜你的号码!\n\n单击任意位置开始!”
self.window=Toplevel(self.master)
帧=帧(self.window)
frame.bind(“,self.guess\u编号)
frame.pack()
self.window.title(“猜数字”)
自窗几何(“400x300+710+390”)
标签=标签(self.window,text=id,font=self.customFont)
标签包装(side=“top”,fill=“both”,padx=20,pady=20)
def猜测_编号(自身):
labelGuess=Label(self.window,text=“选择1到10之间的数字”,font=self.customFont)
时间。睡眠(2)
labelGuess.pack(填充=两者,padx=20,pady=20)
如果名称=“\uuuuu main\uuuuuuuu”:
root=Tk()
视图=窗口(根)
view.pack(side=“top”,fill=“both”,expand=True)
root.mainloop()

您在初始值设定方法中对
guess\u number
的初始调用可能在按下按钮并触发
new\u窗口
事件回调之前被调用。在
guess\u number
中,您试图将
self.window
作为参数传递给
Label()
,但此时它将是未定义的。

首先,您不应该从
\u init\u
方法中创建新属性

也就是说,指出了问题的原因:您在
new\u window
方法中创建了window对象,但没有调用它

您必须先呼叫新窗口,然后再呼叫猜测号码,或者从一个窗口呼叫另一个窗口

我建议您将
window
设置为
None
并调用
new\u window
内部的
\uuuuuuuuu
方法,然后(在此之后)调用
guess\u number

而不是“可能”--“肯定”。它就在
\uuuu init\uuuu
中:
self.guess\u number()