Search code examples
pythonzelle-graphics

Zelle and Graphics.py - how can I keep the graphic window always on top?


Using graphics.py, I am wondering if there is a way to keep the graphics.py canvas/window on top the entire time. When using pycharm, if the input is in pycharm - the graphic gets hidden behind the application. I found a way of doing it in turtle, but cannot seem to incorporate that into graphic.py

Here is the turtle:

rootwindow=turtle.getcanvas().winfo_toplevel()
rootwindow.call('wm','attributes','.','-topmost','1')

Any ideas how to accomplish this in Zelle's?

Here is a simple example, when going to the console in pycharm, the graphic window moves behind pycharm instead of remaining on top

from graphics import *

win = GraphWin("MyWindow", 200, 200)

def cir(radius):
    cir = Circle(Point(30,30), radius)
    cir.setFill("red")
    cir.draw(win)
    return

radius=int(input("what size of circle?"))
cir(radius)
win.getMouse()
win.close()

Solution

  • You can do it basically the same way as with the turtle module, the slightly tricky/hacky part is determining what the root window is. There's at least a couple of ways to do that, but I thought using the universal widget method nametowidget() was the cleanest (note: '.' is always the root's window name in tkinter).

    from graphics import *
    
    
    win = GraphWin("MyWindow", 200, 200)
    rootwindow = win.nametowidget('.')
    rootwindow.call('wm', 'attributes', '.', '-topmost', '1')
    
    def cir(radius):
        cir = Circle(Point(30,30), radius)
        cir.setFill("red")
        cir.draw(win)
        return
    
    radius = int(input("what size of circle?"))
    cir(radius)
    win.getMouse()
    win.close()
    

    Recommendation

    In my opinion it would be better for you to learn how to use the graphics module's Entry object to get keyboard data from the user because that way you wouldn't need to do something hacky like this.