简体   繁体   中英

tkinter Canvas window not opening from Powershell

When running on Win10 the following script against the Python 3.8.2 command line, it works just fine, opens a Tk window and displays a triangle:

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
canvas.create_polygon(10, 10, 10, 60, 50, 35)

However, when running the same script from the PowerShell command line with

& "C:/Program Files (x86)/Python38-32/python.exe" c:/Users/Me/sample.py 

The script terminates without error and without opening the Tk window.

How can I get the Tk window to open while calling the Python interpreter from PowerShell command line?

I believe the issue is that you forgot to add

tk.mainloop()

at the end of your script which keeps the window running.

Let me know if adding this fixes your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM