2类tkinter应用程序中的TypeError

时间:2017-02-14 15:57:37

标签: python tkinter

对于Tkinter的初学者,以及Python中的普通人,很难在tkinter上找到合适的东西。这是我遇到的问题(并开始解决)。我认为问题来自python版本。

我正在尝试在OOP中进行GUI,并且我在组合不同的类时遇到了困难。

假设我有一个“小盒子”(例如菜单栏),并希望将其放入“大盒子”中。在本教程(http://sebsauvage.net/python/gui/index.html)中工作,我正在尝试以下代码

#!usr/bin/env python3.5
# coding: utf-8   
import tkinter as tki

class SmallBox(tki.Tk):
    def __init__(self,parent):
        tki.Tk.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text="small box")
        self.box.grid()
        self.graphicalStuff = tki.Entry(self.box) # something graphical
        self.graphicalStuff.grid()

class BigBox(tki.Tk):
    def __init__(self,parent):
        tki.Tk.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text='big box containing the small one')
        self.graphStuff = tki.Entry(self.box) # something graphical
        self.sbox = SmallBox(self)
        self.graphStuff.grid()
        self.box.grid()
        self.sbox.grid()

但是我收到了以下错误。

File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TypeError: create() argument 1 must be str or None, not BigBox  

2 个答案:

答案 0 :(得分:1)

您使用的教程有一个不正确的示例。 RewriteEngine On RewriteBase / RewriteRule ^/?hello/(news)/ /bye/$1/? [L,NC,R=301] RewriteRule ^index\.php$ - [L] # add a trailing slash to /wp-admin RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L] RewriteRule . index.php [L] 班级没有父母。

此外,您只能创建Tk(或Tk的子类)的单个实例。 Tkinter小部件存在于具有单个根的树状层次结构中。此根小部件为Tk。你不能有一个以上的根。

答案 1 :(得分:0)

代码看起来非常相似:Best way to structure a tkinter application

但是有一点不同,我们在这里没有使用Frame。并且错误要求screenName等中的问题,直观地看起来更像是框架。

事实上,我会说在Python3中你不能再使用第一个教程的版本了,你必须使用Frame,并编写类似的东西:

#!usr/bin/env python3.5
# coding: utf-8
import tkinter as tki
class SmallBox(tki.Frame):
    def __init__(self,parent):
        tki.Frame.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text="small box")
        self.box.grid()
        self.graphicalStuff = tki.Entry(self.box) # something graphical
        self.graphicalStuff.grid()

class BigBox(tki.Frame):
    def __init__(self,parent):
        tki.Frame.__init__(self,parent)
        self.parent = parent
        self.grid()
        self.box = tki.LabelFrame(self,text='big box containing the small one')
        self.graphStuff = tki.Entry(self.box) # something graphical
        self.sbox = SmallBox(self)
        self.graphStuff.grid()
        self.box.grid()
        self.sbox.grid()

if __name__ == '__main__':
    tg = BigBox(None)
    tg.mainloop()

我们找不到(特别是对于法国人,或者可能没有"自然"英语)的许多示例和文档,我使用的那个很常见,所以也许它会是对某人有用。