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

373 views Asked by At

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?

1

There are 1 answers

0
cdlane On

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.