Search code examples
python-2.7tkintertk-toolkitpython-3.9

Python / Tkinter grid_rowconfigure issue between versions


Working with some samples I encountered strange behaviour in Python 3.9.
grid_rowconfigure did not expand rows as i would expect it to.

Unfortunately looking for an already reported bug inside either Python3.9.2 or Tk8.6 lead nowhere.
Please find below the reproduction code.

#!/usr/bin/python
"""
Code Stripped for Stackoverflow.
"""
from sys import hexversion
if hexversion < 0x03000000:
    # pylint: disable=import-error
    # if we are in 0x02whaterver that works
    import Tkinter as tk
    import ttk
else:
    import tkinter as tk
    from tkinter import ttk

class ScrolledTree(ttk.Frame):
    """ A Scrolled Treeview
    """
    def __init__(self, *args, **kwargs):
        # pylint: disable=unused-argument
        if hexversion < 0x03000000:
            ttk.Frame.__init__(self, *args, **kwargs)
        else:
            super().__init__()
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.tree = ttk.Treeview(self)

        self.tree.grid(row=0, column=0, sticky=tk.NW+tk.SE)

        ttk.Style().configure("TFrame", background="red")
        ttk.Style().configure("ScrolledTree.TFrame", background="blue")

class ScrolledFilterTree(ttk.Frame):
    """ A Scrolled Tree with Filters
    """
    def __init__(self, *args, **kwargs):
        # pylint: disable=unused-argument
        if hexversion < 0x03000000:
            ttk.Frame.__init__(self, *args, **kwargs)
        else:
            super().__init__()
        ttk.Label(self, text="asdf").grid(row=0, column=0)
        self.tree = ScrolledTree(self)
        self.tree.grid(row=1, column=0, sticky=tk.NW+tk.SE)
        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(0, weight=1)

        ttk.Style().configure("TFrame", background="red")
        ttk.Style().configure("ScrolledTree.TFrame", background="blue")

def test_show():

    root = tk.Tk()
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.title("Show - ScrolledTree")
    app = ScrolledTree(root)
    app.grid(row=0, column=0, sticky=tk.NW+tk.SE)
    root.mainloop()

    root = tk.Tk()
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.title("Show - ScrolledFilterTree")
    app = ScrolledFilterTree(root)
    app.grid(row=0, column=0, sticky=tk.NW+tk.SE)
    root.mainloop()
#end test_show

if __name__ == "__main__":
    test_show()

What does look strange?

ScrolledText classes (Python2.7 / Tk8.5) on the left (Python 3.9 / Tk8.6) on the right are both fine. (Python2.7 / Tk8.5) on the left (Python 3.9 / Tk8.6) on the right
Using them in ScrolledFilteredText results in: (Python2.7 / Tk8.5) on the left (Python 3.9 / Tk8.6) on the right

Is there any explanation why these look different? Or - to be more precice - behave different on rescaling of the root window?

Update 1

OS: Windows 10 20H2 ( 19042.985 ) x64
Python: 2.7.16 x64 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19)
Python: 3.9.2 x64 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55)
Tk/Tcl: Stock versions from the python distributions.

Any help is very much appreciated.


Solution

  • Since you did not pass *args and **kwargs to super().__init__(), i.e. did not pass the parent argument, ScrolledTree will be put in root window instead of ScrolledFilterTree frame in Python 3.

    Add back *args and **kwargs to super().__init__() will fix the issue.