Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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_Python 2.7_Tkinter - Fatal编程技术网

Python 通过从Tkinter继承的类传递参数

Python 通过从Tkinter继承的类传递参数,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我正在学习如何使用Tkinter制作GUI应用程序,我不明白为什么我不能通过class\uuuu init\uuu函数传递宽度和高度参数 编辑:对不起,我很激动。它所做的是告诉我我传递了太多的论点。无论我如何重新排列类\uuuu init\uuuuu和Frame\uuuu init\uuuuu的参数,都不会发生任何变化。要么太多,要么不够 编辑_2:好的,这个运行没有错误。但它仍然没有调整帧的大小 以下是我的工作内容: #!/usr/bin/python from Tkinter import

我正在学习如何使用Tkinter制作GUI应用程序,我不明白为什么我不能通过class
\uuuu init\uuu
函数传递宽度和高度参数

编辑:对不起,我很激动。它所做的是告诉我我传递了太多的论点。无论我如何重新排列类
\uuuu init\uuuuu
Frame
\uuuu init\uuuuu
的参数,都不会发生任何变化。要么太多,要么不够

编辑_2:好的,这个运行没有错误。但它仍然没有调整帧的大小

以下是我的工作内容:

#!/usr/bin/python

from Tkinter import *


class App(Frame):
    def __init__(self, parent, width, height):
        Frame.__init__(self, parent)
        self.parent = parent
        self.width = width
        self.height = height
        self.initUI()

    def initUI(self):

        self.parent.title("App")
        self.grid()

def main():

    root = Tk()
    app = App(root, 300, 250)
    root.mainloop()

if __name__ == '__main__':
    main()

self.width=width
不会更改帧大小,因为帧使用不同的方法更改大小

第一种方法:您可以使用
框架

Frame.__init__(self, parent, width=width, height=height)
见:

第二种方法:您可以使用
self.config(key=value)
self[“key”]=value

self.config(width=width, height=height)

见:



顺便说一句:
Frame.\uuu init\uuuu
创建
self.master
并将
parent
分配给
self.master
,这样您就可以使用
self.master
而不是
self.parent


顺便说一句:您可以创建不带框架的窗口

from tkinter import *


class App(Tk):

    def __init__(self, width, height): # no need "parent"
        Tk.__init__(self) # no need "parent"

        self["width"] = width
        self["height"] = height

        # OR         
        #self.geometry("%dx%d" % (width, height))
        #self.geometry("%dx%d+%d+%d" % (width, height, x, y))

        self.initUI()

    def initUI(self):

        self.title("App") # no need "parent"
        # no need self.grid()


def main():

    App(300, 250).mainloop()

if __name__ == '__main__':
    main()

您列出的代码对我有用。你能把你遇到的异常的回溯添加到你的问题中吗?嗯,它运行时没有像这样的错误。我删除了一些用于小部件放置的方法,以保持简短。但是除了运行之外,它仍然没有执行我试图执行的任务。对我来说,窗口启动时几乎看不见。然后我必须手动调整大小。您必须设置主窗口大小(
root
是主窗口)或框架大小。但是usin
self.width
不会更改帧大小。你必须使用
self.config(width=new\u width)
Oh lovely!!绝对地非常感谢。现在它就像一个符咒<代码>框架。uuu init uuuu
创建
self.master
,您可以使用它来代替
self.parent
我在向这个类中添加小部件方法时注意到,我实际上不必将窗口(如
root
self.parent
)指定到
.grid
函数中。这是正常的还是因为我使用的是
Frame.\uuuu init\uuuu
?命令
root=Tk()
创建并显示主窗口
Frame
是对其他小部件进行分组的唯一“不可见”(无边框)对象。您可以创建
类应用程序(Tk)
,并将小部件直接添加到主窗口(无
Frame
),此时您不需要
Frame
。当您需要对小部件进行分组并同时显示/隐藏所有小部件的框架时,您将需要
Frame
,或者您需要在一个框架内使用
pack()
,在另一个框架内使用
grid()
(不要在一个框架或窗口中混合使用
pack
grid
)。当您需要创建自己的小部件时,会出现一个错误-
类TwoButtonSandoneLabel(框架)
self["width"] = width
self["height"] = height
from tkinter import *


class App(Frame):
    def __init__(self, parent, width, height):
        Frame.__init__(self, parent)
        self.parent = parent

        #self.config(width=width, height=height)

        self["width"] = width
        self["height"] = height

        self.initUI()

    def initUI(self):

        self.parent.title("App")
        self.grid()

def main():

    root = Tk()
    app = App(root, 300, 250)
    root.mainloop()

if __name__ == '__main__':
    main()
from tkinter import *


class App(Tk):

    def __init__(self, width, height): # no need "parent"
        Tk.__init__(self) # no need "parent"

        self["width"] = width
        self["height"] = height

        # OR         
        #self.geometry("%dx%d" % (width, height))
        #self.geometry("%dx%d+%d+%d" % (width, height, x, y))

        self.initUI()

    def initUI(self):

        self.title("App") # no need "parent"
        # no need self.grid()


def main():

    App(300, 250).mainloop()

if __name__ == '__main__':
    main()