【发布时间】:2018-10-14 23:08:50
【问题描述】:
我创建了一个直接从根窗口继承的简单类,它可以在 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')
【问题讨论】:
-
尝试将
__init__的第一行改成下面的tk.Tk.__init__(self, className=dialog_message)