简体   繁体   中英

How to exit GUI program by clicking the mouse in Python?

from graphics import *
def main():
    win = GraphWin("Shapes")
    center = Point(100, 100)
    circ = Circle(center, 30)
    circ.setFill("red")
    circ.draw(win)

    time.sleep(6) 

main()

So, I have installed (barely somehow) graphics.py by John Zelle so I can follow his book (An Introduction to Comp. Sci.) material for Chapter 5 Objects and Graphics.

I am writing all of my code in Sublime Text editor and when I want to compile, I go to cmd and type: python "name_of_file".py and start the program this way. In this package there is an object Window which is created by invoking GraphWin() (everything will be drawn here in the whole chapter), but that object stays visible for just a split of a second (my guess is because main() is executed and therefore it is done).

On the contrary, if I type all of required code (from that package) in the cmd , that Window object (and everything on it) stays visible the entire time.

It is very inconvenient to type in cmd . Is there something I can type inside main() to keep my work (Window object and everything else) visible, until lets say, I click a mouse or press Enter? I don't know how to implement that in Python.

Use getMouse() method of GraphWin object. It will wait until you click to continue execution.

from graphics import *
def main():
    win = GraphWin("Shapes")
    center = Point(100, 100)
    circ = Circle(center, 30)
    circ.setFill("red")
    circ.draw(win)

    win.getMouse()

main()

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