Python - Printing to Shell with getMouse() - graphics.py - python

I have the very simple program as followed: Python 3.3.2
from graphics import *
def main():
print("Click your mouse to exit")
win = GraphWin()
# Draw graphics here
win.getMouse()
win.close()
main()
This is obviously a very simple program. All I am trying to do is create a function that opens up the graphics window, draws the graphics, and then tells the user in the shell to "click the mouse to exit". However, the problem is that I can't get the print statement to show up in the shell until after I click the mouse and the graphics window closes. So, is there a way to be able to print a statement while using getMouse()?

Related

graphics: How to draw new objects on the screen once it's already up?

I'm making an interactive map for my class, using Zelle's graphics.py . I got the map , and the data all set, but I can't figure out how to get the graphics window to update and draw new objects (route markers) on the map, after recieving user input. I've tried using a for loop and doing the update() method, no luck. Once the window is up, I'm supposed to get user input and the map is supposed to reflect that, and I can't figure out that last little bit.
from graphics import *
win = GraphWin("map" , 500,500)
win.setBackground("black")
textbox = Entry(Point(250,250),10)
textbox.draw(win)
text = textbox.getText()
if text == "hello":
line = Line (Point(100,100), Point(200,100))
line.draw(win)
win.getMouse()
win.close()
So once the desired User input is recieved how do i get the changes to show up on screen?
Your primary issue is that text = textbox.getText() executes before you get a chance to type input. This SO answer discusses this issue of Zelle graphics lacking an event model. What it recommends is you ask your user to click the mouse after entring input to force the program to wait:
from graphics import *
win = GraphWin("map", 500, 500)
textbox = Entry(Point(250, 250), 10)
textbox.draw(win)
win.getMouse()
text = textbox.getText()
if text == "hello":
line = Line(Point(100, 100), Point(200, 100))
line.draw(win)
win.getMouse()
win.close()
You can do that in a loop to get all your locations and use a keyword like "done" to break out of the mouse wait loop.
Your secondary issue is you were drawing a black line on a black background so you'd never see it had it worked correctly.

Start Python script GUI minimized / in Windows Tray | Tkinter

I´ve built a simple GUI app, and I´m playing with pystray.
Actually, my script starts as usual, the first thing you see is the GUI.
If you klick on exit, the GUI minimize, and the tray Icon spawns.
For now, i search a way to start my script in this "tray Mode"
here are some informations:
class Hauptfenster:
# Define a function for quit the window
def quit_window(icon, item):
icon.stop()
fenster.destroy()
# Define a function to show the window again
def show_window(icon, item):
icon.stop()
fenster.after(0, fenster.deiconify())
# Hide the window and show on the system taskbar
#staticmethod
def hide_window():
fenster.withdraw()
image = Image.open(os.path.join(application_path, iconFile))
menu = (item('Beenden', Frontend.Hauptfenster.quit_window), item('Einstellungen', Frontend.Hauptfenster.show_window))
icon = pystray.Icon("name", image, "Quicksafe", menu)
icon.run()
Please ask me if you need some more Information and Thanks a lot !
Best regards
Background:
My programm should lay in the autostart of win10 , but i dont want to minimize the window each time i restart my pc
I just added my method hide_window to my main function,
when script starts, you see something moving on to screen for 2ms.. but it disapears very quick... so thats something I can live with

My turtle program in python acts like it has been clicked

import turtle
window = turtle.Screen()
pen = turtle.Turtle()
def star():
pen.forward(100);
turtle.onscreenclick(star())
window.mainloop()
I am very new to turtle,and python itself, I am just using some of the documentation that I found on the python website, but when I run this program it runs my function star without the screen even being clicked. The end goal of this program would be for me to click then it would run the function, then if I clicked again the screen would clear and the function would be run again.
Thanks for the help!
You need to pass reference to start function into onscreenclick, and make start to accept two arguments:
def star(x,y):
pen.forward(100);
turtle.onscreenclick(star)

How to close the Python turtle window after it does its code?

I'm working on a simple program in Python 3.5 that contains turtle graphics
and I have a problem: after the turtle work is finished the user has to close the window manually.
Is there any way to program the window to close after the turtle work is done?
turtle.bye(), aka turtle.Screen().bye(), closes a turtle graphics window.
Usually, a lack of turtle.mainloop(), or one of its variants, will cause the window to close because the program will exit, closing everything. turtle.mainloop() should be the last statement executed in a turtle graphics program unless the script is run from within Python IDLE -n which disables turtle.mainloop() and variants.
turtle.Screen().mainloop() and turtle.done() are variants of turtle.mainloop().
turtle.exitonclick() aka turtle.Screen().exitonclick() binds the screen click event to do a turtle.bye() and then invokes turtle.mainloop()
Try exitonclick() or done() at the end of the file to close the window .
Add tkinter.mainloop()at the end of the file.
example
import turtle
import tkinter as TK
t = turtle.Pen()
for x in range(100):
t.forward(x)
t.left(90)
TK.mainloop()

How To Make Mini Example For DrawingArea Display Something

I've written a mini example for DrawingArea which, when started, displays nothing. If I insert a raw_input() just for waiting for a keyboard press at a specific place, it functions, so this is a workaround. Here's the code:
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
R = 300
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_default_size(R, R)
drawing_area = gtk.DrawingArea()
window.add(drawing_area)
window.show_all()
gc = drawing_area.get_style().fg_gc[gtk.STATE_NORMAL]
if 0:
raw_input()
drawing_area.window.draw_line(gc, R/10, R/10, R*9/10, R*9/10)
raw_input()
This version doesn't display the drawn line in the opening window; upon pressing enter in the shell, it will just terminate (and remove the window). But if I enable the raw_input() at the if 0: block, it waits twice for an enter in the shell and between the two enters it will display the drawn line (so in general the code works, it seems to be just a weird refresh problem).
I also tried to flush the event queue of GTK using this snippet:
while gtk.events_pending(): # drain the event pipe
gtk.main_iteration()
I inserted it at various places, always to no avail.
I also tried the usual gtk.main() as the last command in the script (of course). But it also didn't help.
How do I do this correctly and why is that raw_input() having that strange side-effect?
You should connect to your drawing area's expose-event signal. That is the only place that you should try to draw on the drawing area; the reason for this is that anything you draw is erased again when the window is minimized or another window moves over it. However, the expose event always happens at the right time so you can keep the drawing up-to-date whenever it is needed.
Like this:
def on_drawing_area_expose(drawing_area, event, data=None):
# ... do your drawing here ...
drawing_area.connect('expose-event', on_drawing_area_expose)
Also check out drawing with Cairo, which is the preferred and more flexible way. Here is a tutorial.

Categories