Following a beginners Python3 tutorial, I am supposed use the graphics module to generate an external GraphWin() window and keep it until win.close() is initiated.
At the moment I don't wish to run win.close() and want to keep it open, but PyCharm's "process finished with exit code 0" shuts it off automatically. How can i solve this?
thank you in advance!!
The Overview at the very beginning of the Zelle graphics documentation explains this:
from graphics import *
def main():
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse() # pause for click in window
win.close()
main()
It's the combination of:
win.getMouse() # pause for click in window
win.close()
That holds the window open until the the user clicks on it. Primitive but effective. I checked and it seems to work fine with PyCharm.
Related
I'm coding a program in Python 3.7 that draws flowers where the user clicks on the screen, but I can't figure out how to detect if the user presses the X button while the functions are still being executed, and then stop all processes and close the window.
I looked for solutions and tried using this answer about using winfo_toplevel but I couldn't make that work either.
My code looks like this:
import turtle, random
from sys import exit
window = turtle.Screen()
window.setup(1000,500)
#my functions to draw go here, but aren't included since the question is about closing the program
def stop():
turtle.bye()
root.destroy()
exit()
window.onclick(chooseFlower)
window.listen()
canvas = window.getcanvas()
root = canvas.winfo_toplevel()
root.protocol("WM_DELETE_WINDOW", stop)
while not root.protocol("WM_DELETE_WINDOW"):
turtle.mainloop()
I get this bunch of errors:
tkinter.TclError: can't invoke "destroy" command: application has been destroyed
and then
raise Terminator
turtle.Terminator
and then
_tkinter.TclError: invalid command name ".!canvas"
What else can I try?
Your code is destroying the window before you click on the X button. The while loop condition is wrong. I think your code should run without that. Also, turtle.bye() will close the window for you without any error. you do not require the destroy function to do so. Try changing code as per below. It should work.
def stop():
turtle.bye()
and comment out while
root.protocol("WM_DELETE_WINDOW", stop)
# while root.protocol("WM_DELETE_WINDOW"):
turtle.mainloop()
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()
I'm new to Python and trying to learn graphics with John Zelle's graphics.py. I'm writing the Python scripts in MacVim and executing from the terminal (Mac OS 10.9.2) on Python 3. If I try to open a new window with GraphWin() the window opens briefly but then immediately closes.
Ex:
from graphics import *
win = GraphWin("circle",500,500)
c = Circle(point(100,100),30)
c.draw(win)
GUI elements with TkInter work just fine.
Any ideas why this might be happening?
Thanks!
If the statements you've shown in your question were entered as a script, and you just ran the script, then the problem is that the window is closed implicitly when the script ends. You will see in the very first example from the documentation (which also appears in the source code for graphics.py itself):
from graphics import *
def main():
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse() # Pause to view result
win.close()
main()
Notice that the author has specifically included a statement to pause before closing the window.
If, instead of a script, you just type the statements in one by one at an interactive Python prompt, then as long as that Python prompt is open, the graphics window stays open (until you explicitly close it, or close the Python session).
I just download a graphics module (found at http://mcsp.wartburg.edu/zelle/python/) and wrote a quick program to test it out. All it is supposed to do is create a window. It works, but the second the window is created Python (not IDLE) goes non-responsive and I have to force quit. What could be causing this? The code (that they provide as an example) is:
from graphics import *
def main():
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse()
win.close()
After I click it suddenly crashes.
This is actually expected behavior. The line
win.getMouse()
hangs the interpreter and window until you click. After the click, the line
win.close()
destroys the window, then your program terminates. This may appear as a "crash" to you, but is actually the expected end of your Python program's run. (If you're getting an error, post the trace in your question.)
To display a wxPython window in full screen mode you use:
ShowFullScreen(True)
How do you get out of full screen though? I've tried the obvious way:
ShowFullScreen(True)
sleep(5)
ShowFullScreen(False)
This doesn't work though. When I run the script, nothing appears. After 5 seconds a window roughly 200x250 appears in the top-left corner of the screen, without anything inside of it. It doesn't appear to have any borders either.
If I change this to
showFullScreen(True)
then I get stuck with a full screen window that I have to use Alt + F2 -> xkill to get out of.
It looks like you need to Show() the window first. (According to the documentation, you shouldn't have to. Maybe this is a bug.) I tested on Mac OS X and Windows - they both exhibit issues if you don't call Show() first.
Also note that you shouldn't sleep in the main GUI thread. You'll hang the UI. Using CallLater is one potential solution, as shown in my example.
Working example:
import wx
def main():
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, 'Full Screen Test')
frame.Show()
frame.ShowFullScreen(True)
wx.CallLater(5000, frame.ShowFullScreen, False)
app.MainLoop()
if __name__ == '__main__':
main()
The documentation for ShowFullScreen reads:
ShowFullScreen(show, style=wx.FULLSCREEN_ALL)
Depending on the value of show parameter the window is either shown full screen or restored to its normal state.
Parameters:
show (bool)
style (long): is a bit list containing some or all of the following values, which indicate what elements of the window to hide in full-screen mode:
wx.FULLSCREEN_NOMENUBAR
wx.FULLSCREEN_NOTOOLBAR
wx.FULLSCREEN_NOSTATUSBAR
wx.FULLSCREEN_NOBORDER
wx.FULLSCREEN_NOCAPTION
wx.FULLSCREEN_ALL (all of the above)
So put your Full Screen toggle event/s in a Menu and start full screen mode with:
self.window.ShowFullScreen(True, style=(wx.FULLSCREEN_NOTOOLBAR | wx.FULLSCREEN_NOSTATUSBAR |wx.FULLSCREEN_NOBORDER |wx.FULLSCREEN_NOCAPTION))
Note that I omitted wx.FULLSCREEN_NOMENUBAR, in this way you will still be able to access the menu to turn full screen mode off again.