local machine _tkinter.TclError: couldn't connect to display ":0"

local machine _tkinter.TclError: couldn't connect to display ":0"

长话短说: 尝试在我的本地笔记本电脑上显示 tkinter 应用程序时出现以下错误 (运行 Pop_OS!)

_tkinter.TclError: couldn't connect to display ":0"

你好世界,

首先: 我刚开始在这里发布问题,所以请多多包涵。如果我的提问有什么不对的地方,请告诉我。我也是新的 ubuntu 用户(几个月了)。

现在,谈谈我的问题。当我尝试在笔记本电脑上显示 tkinter 应用程序时,出现以下错误:

No protocol specified
No protocol specified
Traceback (most recent call last):
File "/home/lucenden/python/sublime/conversions/conversion_app.py", line 70, in
root = Tk()
File "/usr/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0"

我试着调查这个问题,但唯一与我的问题相关的是人们使用 SSH 将应用程序流式传输到不同的显示器,而我试图在我的笔记本电脑上显示它自己。

事实是,我之前通过应用我在网上找到的一些解决方案解决了这个问题。但我不知道我尝试过的哪种解决方案实际上起到了作用。这是我到目前为止尝试过的:

Install Xorg

Set $DISPLAY to: ":0:0" and "localhost:0:0"

Dig into tkinter file itsself (nothing useful there from what i can tell)

Turning laptop of and on, but only after applying all fixes at once...

关于我的 system/environment 的信息:

Using Pop_OS! (Ubuntu dist) and Sublime Text to run the code

Running python3.7

同样,如果我遗漏了任何需要的信息,请告诉我。提前致谢!

我的代码:

from tkinter import *
from tkinter.colorchooser import askcolor
import sys


class App(Frame):
""" This is the class for our root window. """
def __init__(self, master=None):
    Frame.__init__(self, master)        # Parameters that you want to send through the Frame class.
    self.master = master
    self.default_bg = "#8f8f8f"
    self.default_w = 0
    self.default_h = 0
    self.pack(fill=BOTH, expand=1)

    # Creating a menu instance.
    menu = Menu(self.master)
    self.master.config(menu=menu)

    # Create the File menu object. Then add a cascade to the menu bar.
    file = Menu(menu)
    # Add commands to the File menu, calling it something, and then specifying the command it runs.
    file.add_command(label="Exit", command=self.app_exit)
    file.add_command(label="Temp", command=self.do_nothing)
    # Then add it to the menu bar.
    menu.add_cascade(label="File", menu=file)

    # Create the Astronomy menu object.
    edit = Menu(menu)
    # Add commands to the Astronomy menu, calling it something, and then specifying the command it runs.
    edit.add_command(label="Clear Master", command=self.clear_master)
    edit.add_command(label="Temp", command=self.do_nothing)
    # Then add it to the menu bar.
    menu.add_cascade(label="Edit", menu=edit)

    self.init_app()

@staticmethod
def do_nothing():
    print("Do nothing")

@staticmethod
def app_exit():
    exit()

def clear_master(self):
    """ Clear the master of any widgets on the screen. """
    widget_list = self.master.winfo_children()
    for widget in widget_list:
        widget.pack_forget()

def track_mouse(self):
    print("COME BACK TO track_mouse !!!!")

def scale(self):
    scale = Scale(self.master, from_=0, to=10, orient=HORIZONTAL)
    scale.grid()

def init_app(self):
    canvas1 = Canvas(self, width=self.default_w, height=self.default_h)
    canvas1.create_line(10, 0, 10, 600)
    Scrollbar(canvas1)

    button_1 = Button(self.master, text="Exit...", command=self.app_exit)

    canvas1.pack()
    button_1.pack()


root = Tk()
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
app = App(root)
root.mainloop()

@stovfl 你是英雄! 以下是这项工作

export DISPLAY=unix$DISPLAY

相关 post 中提供的解释非常有帮助。 感谢回复!