pythonzelle-graphics

How do I display a variable in a Zelle graphics window?


I have some int variables that I need to display into a Zelle graphics window in Python. I'm trying to do this with a GraphWin() window but I have not found a way. How can I take my integers and display them in a window.


Solution

  • You can display an int in Zelle graphics by passing it as the text argument to a Text object, along with where you want it placed. You can also select the characteristics of the font:

    from graphics import *
    
    win = GraphWin("My Number")
    
    number = 1729
    
    text_1 = Text(Point(100, 100), number)
    text_1.setFace('arial')
    text_1.setSize(18)
    text_1.setStyle('bold')
    text_1.setTextColor('red')
    
    text_1.draw(win)
    
    win.getMouse()  # pause for click in window
    win.close()
    

    You don't need to convert the number to a str first, the Text object appears to handle that for you.

    enter image description here