Python tkinter - cloning root window properties and applying them to top level windows

toplevel tkinter
tkinter grid
tkinter pack
tkinter frame
tkinter root attributes
tkinter place
tkinter toplevel position
tkinter python 3

i want to create multiple nearly same looking windows without copy/paste all window properties in every function.

def child_window():
    childwindow.geometry("400x800")

def window_one():
    childwindow = Toplevel(root)
    childwindow.title("title window one")
    child_window()

def window_two():
    childwindow = Toplevel(root)
    childwindow.title("title window two")
    child_window()

The code doesn't work as the childwindow won't take over the geometry property out of the general function child_window. Is there any possible way to copy the design to all windows without putting the whole code in every single function.

If you pass the Toplevel object to the child_window function, you can use the object to directly set the geometry of that Toplevel window (also please don't reuse variable names):

def child_window(window):
    window.geometry("400x800")

def window_one():
    childwindow = Toplevel(root)
    childwindow.title("title window one")
    child_window(childwindow)

def window_two():
    childwindow = Toplevel(root)
    childwindow.title("title window two")
    child_window(childwindow)

Toplevel Window Methods, Python tkinter - cloning root window properties and applying them to top level windows. toplevel tkinter tkinter grid tkinter frame tkinter pack tkinter python tkinter​  This group of methods are used to communicate with the window manager. They are available on the root window (Tk), as well as on all Toplevel instances. Note that different window managers behave in different ways. For example, some window managers don’t support icon windows, some don’t support window groups, etc. Patterns Displaying Windows

I agree with fhdrsdg on his solution however I would change one thing. Instead of the function holding a single set value for size have that function check the size of the root window first and then apply the same size to the toplevel window.

Your main issue can be solved with a few methods. One method is to pass the toplevel window to the function responsible for assigning geometry. You can also build a class (IMO the best option for various reason) and you can also use global to manage your top windows in the global namespace.

Like this:

import tkinter as tk

def child_window(window):
    window.geometry("{}x{}".format(root.winfo_width(), root.winfo_height()))

def window_one():
    childwindow = tk.Toplevel(root)
    childwindow.title("title window one")
    child_window(childwindow)

def window_two():
    childwindow = tk.Toplevel(root)
    childwindow.title("title window two")
    child_window(childwindow)

root = tk.Tk()
root.geometry("500x300")
# I added this delay due to an issue tkinter has with reading the root size before the mainloop has had a change to fully load the root window.
root.after(1000, window_one)
root.after(1000, window_two)

root.mainloop()

For an OOP approach see this example:

import tkinter as tk


class Example(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("500x300")
        self.after(1000, self.window_one)
        self.after(1000, self.window_two)

    def child_window(self, window):
        print("{}x{}".format(self.winfo_width(), self.winfo_height()))
        window.geometry("{}x{}".format(self.winfo_width(), self.winfo_height()))

    def window_one(self):
        childwindow = tk.Toplevel(self)
        childwindow.title("title window one")
        self.child_window(childwindow)

    def window_two(self):
        childwindow = tk.Toplevel(self)
        childwindow.title("title window two")
        self.child_window(childwindow)

if __name__ == "__main__":
    Example().mainloop()

tkinter.ttk — Tk themed widgets, They are available on the root window (Tk), as well as on all Toplevel instances. For example, some window managers don't support icon windows, some don't support window groups, etc. In Python 2.4, this wrapper is incomplete, and the application must use a To remove the property, set it to an empty string. Multiple root windows with Tkinter I have a module which creates a Tkinter borderless window as one of its helper functions. To improve responsiveness, I created the root (with tk.Tk() ) and stored a reference in a module-wide object.

Your code needlessly repeats itself and fails the DRY code principal, here is solution that fixes the issue and follows the principal.

from tkinter import *
from tkinter.ttk import *

def main():
    root = Tk()

    tops = list()
    for i in range(2):
        tops.append(same_top(root, 'title window ' + str(i)))

    mainloop()


def same_top(root, toptitle):
    child = Toplevel(root)
    child.title(toptitle)
    child.geometry("400x800")

    return child


if __name__ == '__main__':
    main()

Your problem is that your child function does not know who childwindow is, if you would pass childwindow to the geometry function then it would work.

Your code fixed:

from tkinter import *
from tkinter.ttk import *

def main():
    root = Tk()

    window_one(root)
    window_two(root)

    mainloop()


def child_window(childwindow):
    childwindow.geometry("400x800")

def window_one(root):
    childwindow = Toplevel(root)
    childwindow.title("title window one")
    child_window(childwindow)

def window_two(root):
    childwindow = Toplevel(root)
    childwindow.title("title window two")
    child_window(childwindow)


if __name__ == '__main__':
    main()

Using PAGE, Using the Ttk widgets gives the application an improved look and feel. This is a write-only option: setting it changes the widget state, but the Widget.state() method does not Windows and Mac have a notion of an “active” or foreground window. Enable keyboard traversal for a toplevel window containing this notebook. Root Window – Simplest GUI. The root window is just like a piece of paper on which you can draw graphical objects. The actual UI construction is pretty standardized. You create a root window, add widgets and then add functionality to widgets. Let’s bisect it in a bit more detail. Following are the steps you are required to follow: Create a

Python - Tkinter Toplevel, It will yield a Python module which implements the application interface. In Windows one may also start PAGE from the command line by going to the installation Adjust properties of the widgets and the toplevel window as desired​. def init(top, gui, *args, **kwargs): global w, top_level, root w = gui top_level = top root  First Tkinter Window # File: hello1.py from Tkinter import * root = Tk() # Create the root (base) window where all widgets go w = Label(root, text="Hello, world!") # Create a label with words w.pack() # Put the label into the window root.mainloop() # Start the event loop

File: android-tkinter/CODE/textEditor.py, Toplevel widgets work as windows that are directly managed by the window manager. They do not necessarily have a parent widget on top of them. 25.1.5. How Tk and Tkinter are Related¶ From the top down: Your App Here (Python) A Python application makes a tkinter call. tkinter (Python Package) This call (say, for example, creating a button widget), is implemented in the tkinter package, which is written in Python. This Python function will parse the commands and the arguments and

graphics.py, The fix now reads a spawned 'which python' # command to be path agnostic when hardcoding Try the . import first: it's crucial that this gets its own version. Note that passing a "master=self" argument has no effect on root window lifts, and and process; caveat: Popup is the same as Clone for Toplevel popup windows;  Tkinter uses the term ‘root window” for a graphical window which accommodates all the widgets. The first step in Tkinter GUI designing is to create a root window. To create a new root window: from Tkinter import * root = Tk(className="My first GUI") # creates root window # all components of thw window will come here root.mainloop() # To

Comments
  • Your child_window() function does not know about the childwindow toplevel window. This is for 2 reasons. Both functions are only working in the local namespace and thus don't share an info.
  • You could also keep the dimensions in the child_window function and call child_window(root) right after the creation of root to give all windows the same size.