Hi everyone,

I'm feeling a little dizzy trying to place the root window of my Tkinter app on the screen.
I followed mawe advice and coded like this:

root = Tk()
root.config(bg="white")
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))

However the window gets placed wrongly and partially out of the screen.

thank you,

Gab

I tried one small app and the method above seems to work smoothly so I can't guess why that weird behaviour when I do the same with my app...

Moreover, how can I force all my widget to be placed on the top left corner of the window?
If you run the following code you will see that the widgets stay just in the centre of the window. Shouldn't "sticky" force them to align as indicated?

from Tkinter import *

root=Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))

class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.grid(sticky=NW)

        button = Button(frame, text="Hello!", bg="green")
        button.grid(sticky=NW)
        
app = App(root)
root.mainloop()

thanks again,

Gab

Take a look at this, it might explain the limitations of the grid layout manager ...

# the size of a grid cell in a given row or column is determined
# by its largest widget
# if you give a grid layout more space than it needs, it will
# fill this space from its center on out

from Tkinter import *

root=Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))

class App:
    def __init__(self, master):
        frame1 = Frame(master, height= 100, width=200, bg='yellow')
        frame1.grid_propagate(0)
        # sticky does nothing in this situation
        frame1.grid(row=0, column=0, sticky=NW)

        frame2 = Frame(master, height= 100, width=200, bg='red')
        frame2.grid_propagate(0)
        frame2.grid(row=0, column=1, sticky=NW)

        button = Button(frame1, text="Hello!", bg="green")
        # this is a child grid
        # sticky does nothing in this situation
        button.grid(row=0, column=0, sticky=NW)

        button2 = Button(frame2, text="Hello!             ", bg="green")
        # sticky does nothing in this situation
        button2.grid(row=0, column=0, sticky=NW)
        button3 = Button(frame2, text="Hello!", bg="green")
        # now the sticky makes sense
        # because of the larger cell width created by button2
        button3.grid(row=1, column=0, sticky=NW)

app = App(root)
root.mainloop()

To make things work the way you want it you have to do it this way ...

# using place() for the frame will make grid() work
# correctly within the frame

from Tkinter import *

root=Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))

class App:
    def __init__(self, master):
        frame = Frame(master, bg='yellow')
        frame.place(x=0, y=0)

        button1 = Button(frame, text="Button1             ", bg="green")
        button1.grid(row=0, column=0)
        button2 = Button(frame, text="Button2", bg="green")
        # now the sticky makes sense because of
        # the larger cell width created by button1
        button2.grid(row=1, column=0, sticky=NW)

app = App(root)
root.mainloop()

For some reason I did miss your answer...

Thank you very much for having clarified my problem.
You're right: the grid layout manager is better than pack but not better than palce.
I started using grid 'cause it seemed to me less tricky but now I'm somehow facing it's limitations.
Next time I'll try place.
I've decided to walkaround the problem and let the main window stay sticked to the child widget's dimension. I've also removed the possibility for the user to resize the window and all the other top level widgets with this: root.resizable(0,0)

By the way I'm positively impressed of Tkinter GUI manager. Lots of people say it's crap but I found it honest and quick for certain kind of applications.

Thanks again,

Gab

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.