简体   繁体   中英

Move Toplevel with place geometry manager

Once an application has been created and displayed on the screen, is it possible to move the application to a different location on the screen?

I have a library function which places the application or a Toplevel frame like this

def create_top_window(master, node, center=True):
    if not master:
        master = TK.Toplevel()
    if center:
        center_window(master, node.width, node.height)
    node.widget = TK.Frame(master, relief=node.relief)
    node.widget.place(x=node.x, y=node.y,width=node.width, height=node.height)

def center_window(root, w=300, h=200): # Center main application window
    # set screen width and height
    ws = root.winfo_screenwidth()
    hs = root.winfo_screenheight()
    # calculate position x, y
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))

The node is an object with other attributes other than those needed by the widget - and will contain the required size of the application.

I tried calling place again but that did nothing. How do I change the position on the screen at a later date in the program?

place is strictly for locating widgets within other widgets.

To move a toplevel or root window you can use the geometry command. For example, to move a window to the upper-left corner you can to this:

root.geometry("+0+0")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM