Python2.7 Tkinter包,范围和类结构

时间:2016-03-13 19:26:24

标签: python-2.7 tkinter

我在Python 2.7中使用Tkinter并且遇到了我的类结构问题。这是代码:

Traceback (most recent call last):
  File "main_oop.py", line 31, in <module>
    main()
  File "main_oop.py", line 26, in main
    window = Display(root)
  File "main_oop.py", line 20, in __init__
    self.message = tk.Message(self, text=self.messageString, bg="#000000", font=parent.displayFont, fg="#777777", justify="c")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2817, in __init__
    Widget.__init__(self, master, 'message', cnf, kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2081, in __init__
    BaseWidget._setup(self, master, cnf)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2059, in _setup
    self.tk = master.tk
AttributeError: Display instance has no attribute 'tk'

我收到以下错误:

Traceback (most recent call last):

 File "/home/sarath/virtualenvs/tensorflow/bin/tensorboard", line 9, in module> load_entry_point('tensorflow==0.7.1', 'console_scripts', 'tensorboard')()


  File "/home/sarath/virtualenvs/tensorflow/local/lib/python2.7/site-packages/tensorflow/tensorboard/tensorboard.py", line 82, in main
    purge_orphaned_data=FLAGS.purge_orphaned_data)

TypeError: __ init__() got an unexpected keyword argument 'purge_orphaned_data'

代码一直有效,直到我包含 init 方法的最后四行,从使用tkFont的行开始。

1 个答案:

答案 0 :(得分:1)

您正尝试将Display的实例作为另一个小部件(...tk.Message(self,...))的父级。只有tkinter小部件可以是其他小部件的父级,self不代表小部件。

在这种特定情况下,您必须使用self.parent作为第一个参数:

self.message = tk.Message(self.parent, ...)