我创建了一个直接从根窗口继承的简单类,它在Mac机器上运行完美。但是,当我尝试在我自己的基于Ubuntu的机器上运行它时,它崩溃并出现以下错误。造成这种情况的原因是什么?
Traceback (most recent call last):
File "/home/estilen/Dropbox/Python/email_viewer/dialog.py", line 15, in <module>
Dialog('This is definitely working')
File "/home/estilen/Dropbox/Python/email_viewer/dialog.py", line 7, in __init__
tk.Tk.__init__(self, dialog_message)
File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "This is definitely working"
代码:
import tkinter as tk
class Dialog(tk.Tk):
def __init__(self, dialog_message):
tk.Tk.__init__(self, dialog_message)
self.geometry('400x100')
label = tk.Label(self, text=dialog_message)
label.pack(anchor='center', side='top', pady=10)
self.mainloop()
if __name__ == '__main__':
Dialog('This is definitely working')
答案 0 :(得分:4)
tk.Tk.__init__(self, dialog_message)
这对我来说并不合适。 Tk构造函数的第一个位置参数是screenName
。如果您不想在名为&#34;的显示屏上显示您的窗口,这肯定有效,那么就不要提供该论点。
tk.Tk.__init__(self)
...或者,更具有惯用力,
super().__init__()