local machine _tkinter.TclError: couldn't connect to display ":0" [duplicate]
Asked Answered
C

1

9

TLTR: Getting the following error while trying to display tkinter app on my local laptop (running Pop_OS!)

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

Hello world,

First off: Im new to posting questions here so bear with me. If something about my questioning isn't right please tell me. Im also a fresh ubuntu user (for a few months now).

Now, onto my problem. When i try to display a tkinter application on my laptop i get the following error:

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"

I tried looking into the problem but the only things related to my problem are from people using SSH to stream there application to a different display monitor, while im trying to display to my laptop its self.

The thing is, i fixed this problem before by applying some solutions i found online. But i dont know which of the solutions i tried actually did the job. Here is what i tried so far:

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...

Info about my system/environment:

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

Running python3.7

Again, if ive left out any needed information please tell me. Thanks in advance!

My code:

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()
Carmagnole answered 23/11, 2019 at 10:2 Comment(1)
Relevant _tkinter.TclError: couldn't connect to displayZwinglian
C
4

@stovfl you're a hero! The following did the job

export DISPLAY=unix$DISPLAY

The explanation provided in this post was really helpful.

Carmagnole answered 23/11, 2019 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.