Reputation: 5734
When i run basic code in the native python editor, the canvas stays open.
When i do the same python code using visual studio code (press F5 to run), the application runs, but then instantly closes and reverts back to the editor.
Here is an example of code:
import sys
from tkinter import *
def mHello():
mtext = ment.get()
mlabel2 = Label(mGui, text = mtext).pack()
for i in range(0, 10):
Label(mGui, text = "hello " + str(i+1)).pack()
mGui = Tk()
ment = StringVar()
mGui.geometry('450x450+500+300')
mGui.title("my test thing")
mlabel = Label(mGui, text = "press to okay button to run the code").pack()
mbutton = Button(mGui, text = "ok", command = mHello, fg = "white", bg = "blue").pack()
mEntry = Entry(mGui, textvariable = ment).pack()
How can i get VS code to behave the same as the native editor please ?
Upvotes: 0
Views: 1207
Reputation: 5734
adding input() at the end of the code works.
input("press enter to close >>>")
Upvotes: 1
Reputation: 765
Visual studio code uses the python interpreter, which compiles your script before running it. Your window closes immediately because you don't call mainloop() on your window. This means as soon as the window is drawn, the program ends, and the window closes.
Your window doesn't close in IDLE because it runs in the interactive shell. The shell waits for commands, so it leaves your window open. See this question for more details: When do I need to call mainloop in a Tkinter application?
All you need to do is add:
mGui.mainloop()
To the end of your script.
Upvotes: 2