Position reopening screen Python Tkinter

Asked

Viewed 100 times

0

I am developing a program in Python and use Tkinter to create a screen as below:

    import tkinter as tk

    def on_closing():
        cpos = str(root.winfo_rootx()) + '+' + str(root.winfo_rooty())
        # salvo o cpos em arquivo com as coordenadas
        root.destroy()

    if __name__ == '__main__':
        root = tk.Tk()
        root.title("teste")
        cpos = '0+0'
        #tratamentos para carregar o arquivo com as coordenadas
        # larg x alt + x coord + y coord
        root.geometry("250x500+"+cpos)
        root.protocol("WM_DELETE_WINDOW", on_closing)
        root.mainloop()

I can save, recover and use the saved position again. But this position always seems a difference of a few pixels, IE, I can never make the screen always open in the same place. I do not know if the problem is at the time to save the position or use again in .geometry. Any tips? Thank you!!

1 answer

0

I use it this way, but you can adapt it to your way.

janela = Tk()
janela.resizable(0, 0)


# dimensoes
largura = 550
altura = 500

# resulução do sistema
larguta_tela = janela.winfo_screenwidth()
altura_tela =  janela.winfo_screenmmheight()

# posicao da tela
posix = larguta_tela/2 - largura/2
posiy = altura_tela/2 - altura/2

# definir a geometry
janela.geometry("%dx%d+%d+%d" % (largura,altura,posix,posiy))
  • Hi Maciel, in your example the positioning of the screen will always be the same. What I need is to return to the last position of the screen on which the program was before it was closed. As I said, saving and restoring information is not the problem, but rather the return to. Geometry at position x and y. Always gets a few pixels apart. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.