1

I'm trying to make a GUI application with tkinter in vscode but when I run my program I don't get a window.

My code is as follows:

from tkinter import *

window = Tk()
greeting = Label(text="Hello, World")

greeting.pack()
window.mainloop()

Running tkinter with just Python in the command line works, but not when I try in vscode. Any help would be hugely appreciated!

11
  • Have you set a Python interpreter in VS Code? What happens if you run the command python -m tkinter from VS Code? Commented Feb 22, 2023 at 20:30
  • @JRiggles Terminal moves to a new line without a cursor. Which is the same as when I run the program. It's acting like it's running something and waiting for input. Commented Feb 22, 2023 at 20:34
  • Does a tkinter window open up somewhere? That command should start a very basic tk app. The same should be true for your app - when you run it in VS Code, the app window should open. Check your taskbar for any new windows. Commented Feb 22, 2023 at 20:36
  • @JRiggles I've checked my taskbar and task manager and nothing is there. Commented Feb 22, 2023 at 20:38
  • If you run that command in a regular Command Prompt, does the example app show up? Commented Feb 22, 2023 at 20:40

1 Answer 1

2

I finally figured out what the problem is. You run the program, there is no error whatsoever, and it appears to be running, but at the same time, there is no window.

For me, I had installed VS Code for doing the Harvard course CS50 Python. I was trying to use Tkinter for my final project. Here's what I figured out, almost by accident.

enter image description here

CS50 has VS Code setup to run on a virtual server. As a result, when you run your program that uses a GUI, the GUI window is actually created, but on the virtual server.

Go to the left side buttons in VS Code, and you'll find one that just says CS50, and if you mouse over it, it will say "CS50 MENU". Click on that. You'll then see a bunch of setting, and one will say "Launch noVNC client to view your GUI program" with a big (in my version) blue button that just said "GUI" on it.

When you click that button, it will open up a virtual server window in your web browser (FireFox for me), and you will see the GUI window you created there.

This was 2 full days of banging my head against a wall over and over, because there was no error, but no window. I'm pretty sure this is the problem me and other people with VSCode are having, because we don't realize we're operating on a virtual service, and our window is actually being created, but on the virtual server, not on our computers. Hope I helped someone going quietly insane trying to figure this out.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was wondering why this was happening!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.