Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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麻烦:我的GUI没有属性包?_Python_User Interface_Python 3.x_Tkinter - Fatal编程技术网

Python Tkinter麻烦:我的GUI没有属性包?

Python Tkinter麻烦:我的GUI没有属性包?,python,user-interface,python-3.x,tkinter,Python,User Interface,Python 3.x,Tkinter,这里有个叫Tkinter的人想用Tkinter建造扫雷舰 运行代码时,我收到一个完美的电路板和一条错误消息,上面说AttributeError:“GUI”对象没有属性“pack” 发生了什么事 from tkinter import * #code class GUI: def __init__(self, master, rows, cols, box_grid): self.master = master self.rows = rows

这里有个叫Tkinter的人想用Tkinter建造扫雷舰

运行代码时,我收到一个完美的电路板和一条错误消息,上面说AttributeError:“GUI”对象没有属性“pack”

发生了什么事

from tkinter import *

#code

class GUI:
    def __init__(self, master, rows, cols, box_grid):
        self.master = master
        self.rows = rows
        self.cols = cols
        self.box_grid = box_grid
        self.create_buttons()

    def left_click_callback(box):
        #code

    def right_click_callback(box):
        #code

    def create_buttons(self):
        #code

#more code


a = create_boxes(20,20,100)
b = create_grid(20,20,a)

root = Tk()

board = GUI(root, 20, 20, b)
board.pack()
root.mainloop()

提前谢谢

因为您的GUI类是一个对象,而不是具有pack属性的小部件。此类应继承自
框架
。如果要将属性设置为帧,请使用
super()。\uuu init\uuu(**args)
。您必须使用
super
来构造框架,如果您不希望对其应用任何内容,则可以将其保留为空

class GUI(Frame):
    def __init__(self, master, rows, cols, box_grid):
        # Example of setting Frame properties
        super().__init__(relief=tk.GROOVE, borderwidth=5, bg='Lime') 
如果您希望该类中的小部件位于该类组中,那么该类中的小部件也应该使用self作为包位置

创建按钮的示例

Button(self, text='My Button', ... )

你为什么认为
的方法?你没有给它这个名字,如果你想要一个如何实现你的游戏的例子,看看Python食谱中的。