Way to set a singled dimension of the root window using python - tkinter?

208 views Asked by At

I'm looking to have a window occupy the width of the screen but not (yet) the height of the screen. I have the root established and working but as far as I can tell the geometry method requires at least a width AND height, not one or the other individually. I would like the window to continue to scale dynamically in height as widgets are added/taken away. There is a starting height of the notebook tab and button

Without the rest of the code for the ReaderUI class shown below, this is what the main method looks like so far...

def main():
    root=Tk()
    tab_control = ttk.Notebook(root)
    first_tab = ttk.Frame(tab_control)
    tab_control.add(tab1,text="File 1")
    tab_control.pack(expand=1,fill="both")
    new_tab_button = Button(tab,text="create new tab", command = lambda: new_tab(tab_control))
    new_tab_button.pack(padx = 5, pady = 5)
    newUI = ReaderUI(tab1)
    width=root.winfo_screenwidth()
    height = root.winfo_screenheight()
    root.geometry("{}x{}+0+0".format(width,height)
    root.resizable(False,False)
    root.mainloop()

My question boils down to this: is there a way to have the UI build itself in terms of height (as it would without the geometry method in place) and only establish a width as wide as the screen? Potentially a modification to the line root.geometry("{}x{}+0+0".format(width,height)) so that height is left unset and free to move.

1

There are 1 answers

2
Connor On

you can use

winfo_screenwidth - Returns a decimal string giving the width of window's screen, in pixels.

Code

root.winfo_screenwidth()

root being your Tk() instance

then you can lock the x axis by using

root.resizable(False, True) # (X,Y)

Recourse https://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tk8.0a1/winfo.n.html