Tkinter pack()错误参数方法

时间:2017-10-18 22:14:03

标签: python tkinter pack

我尝试使用TKinter和OOP(新手)制作GUI。

self.lbl.pack(alinhamento)

当我尝试在labelCreate方法中传递和参数runfile('D:/Dropbox/Dropbox/Python/Tkinter/db Teste/App.py', wdir='D:/Dropbox/Dropbox/Python/Tkinter/db Teste') Reloaded modules: usuarios, Banco Traceback (most recent call last): File "<ipython-input-73-c94772663ba8>", line 1, in <module> runfile('D:/Dropbox/Dropbox/Python/Tkinter/db Teste/App.py', wdir='D:/Dropbox/Dropbox/Python/Tkinter/db Teste') File "D:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile execfile(filename, namespace) File "D:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "D:/Dropbox/Dropbox/Python/Tkinter/db Teste/App.py", line 44, in <module> Application(root) File "D:/Dropbox/Dropbox/Python/Tkinter/db Teste/App.py", line 27, in __init__ lbl1 = self.labelCreate(container1, 'Informe os dados abaixo:', 'Calibri', '9', 'bold', LEFT) File "D:/Dropbox/Dropbox/Python/Tkinter/db Teste/App.py", line 32, in labelCreate self.lbl = Label(container, text) File "D:\Anaconda\lib\tkinter\__init__.py", line 2760, in __init__ Widget.__init__(self, master, 'label', cnf, kw) File "D:\Anaconda\lib\tkinter\__init__.py", line 2289, in __init__ classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)] AttributeError: 'str' object has no attribute 'items' 时收到此错误:

WM_IME_KEYLAST

1 个答案:

答案 0 :(得分:0)

您没有正确调用Label小部件构造函数。这是一些documentation

所以这里是如何做到的(text=text可能看起来很奇怪,但那是因为你给labelCreate()函数的位置参数提供了与Label小部件相同的名称构造函数的关键字参数:

    def labelCreate(self, container, text, fonte, tamanho, estilo, alinhamento):
#        self.lbl = Label(container, text)      # Wrong calling sequence!
        self.lbl = Label(container, text=text)  # Correct way.
        self.lbl['font'] = (fonte, tamanho, estilo)
        self.lbl.pack(side=alinhamento)