-1

Very new to python, just trying to create a Canvas in python using tkinter:

from tkinter import *

top = Tk()
w = Canvas (top , height = 300, width = 300)

Yields error: _tkinter.TclError: no display name and no $DISPLAY environment variable

Most of what I saw regarding this issue had to do with running code on a remote machine and not having a place to display the output. I was running this on the online compiler:

https://repl.it/repls/MintyHumongousParentheses

Any insight into this error is appreciated, thank you!

MapleMatt
  • 17
  • 2
  • 1
    Possible duplicate of [How do I define root for tkinter in Python 3? I'm using an online Python editor](https://stackoverflow.com/questions/44436064/how-do-i-define-root-for-tkinter-in-python-3-im-using-an-online-python-editor) – stovfl Nov 13 '18 at 15:35
  • If where you are executing the code does not contain a display you will not be able to run tkinter. Your online compiler will ending having the same issue as well. Unless it contains some kind of virtual display **(the console window does not count)**. – Mike - SMT Nov 13 '18 at 15:46

1 Answers1

1

The tkinter package is a thin object-oriented layer on top of Tcl/Tk. The problem is Tcl can't find the virtual display.

As stovfl pointed out your problem is the online python editor, I don't know witch one you are using, but repl.it is compatible with remi.gui, see example: https://repl.it/@amasad/tictactoe.

I tested your code locally and it didn't work, so I made a few changes:

from tkinter import *

top = Tk()
w = Canvas(top, width=300, height=300)
w.pack()
top.mainloop()

How do I define root for tkinter in Python 3? I'm using an online Python editor

Tkinter

Python Tkinter not working in a .py file

Griner
  • 151
  • 1
  • 8