Python 通过从Tkinter继承的类传递参数
我正在学习如何使用Tkinter制作GUI应用程序,我不明白为什么我不能通过classPython 通过从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
\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
是主窗口)或框架大小。但是usinself.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()