How do you fix tkinter in python3.8 on wsl?

Question:

I was trying to learn GUI based python therefore i was using the Tkinter library. My OS is windows but I have installed Ubuntu wsl as my default terminal, and use wsl vscode as my default text-editor.
I was just creating a basic window using this sample code to check whether it works or not:

from Tkinter import *
def onclick():
pass
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()
text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="black", foreground="green")
root.mainloop()

Turns out it does not work with python3.8.
Terminal error message
I looked up multiple resources and forums and could not find any proper solutions. Here are couple of links i have referred to:
https://realpython.com/python-gui-tkinter/ https://tkdocs.com/tutorial/install.html https://askubuntu.com/questions/1224230/how-to-install-tkinter-for-python-3-8 .
A work around I discovered was that installed anaconda which uses python3.7 and used the windows terminal instead (using pycharm not vscode). And it looks like it is functioning properly.
GUI with python3.7

The thing is.. I like wsl and vscode far better than windows command line, and I wanted to revert back to it. Is there any solution? I really do not want to fool around with the path too much because I had a bad experience previously (But i am willing to do it again if its going to fix my problem). Thanks for hearing me out.

Asked By: Amogh BIju

||

Answers:

The problem isn’t Python, but WSL. You can’t run anything graphical easily inside WSL. (Microsoft is planning to add that feature, but it isn’t ready yet. There are third-party solutions, but they’re not that easy to set up.)

The easiest solution is to use a Windows install of Python 3.8 to run your tkinter app. You can still invoke that from inside your Ubuntu WSL, same as any Windows executable.

Answered By: gilch

WSL now supports graphical interfaces. In order to use it you need to install or update WSL more info. Then run your app as usual and your program window should open.

Answered By: Pokrt