_tkinter.TclError: couldn't connect to display ":0.0" error on AWS server

Viewed 29

I am trying to run a script containing tkinter module on aws ec2 instance and faced with this error _tkinter.TclError: couldn't connect to display ":0.0" When I run the code on local computer, its working fine. Below is the code, if anyone have any idea, would help alot. Thank you in advance!

from tkinter import filedialog
from tkinter import *
import os

if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')

def browse_button():
    # Allow user to select a directory and store it in global var
    # called folder_path
    global folder_path
    filename = filedialog.askdirectory()
    folder_path.set(filename)
    print(filename)


root = Tk()
folder_path = StringVar()
lbl1 = Label(master=root, textvariable=folder_path)
lbl1.grid(row=0, column=1)
button2 = Button(text="Browse", command=browse_button)
button2.grid(row=0, column=3)

mainloop()
0 Answers
Related