#๐Ÿ”’ Trying to child class a tkinter widget, however setting width and height attributes isn't working.

23 messages ยท Page 1 of 1 (latest)

quasi needle
#

As in the title, in the constructor of the child class I'm trying to set a predetermined width and height, but when a create the widget those attributes are no taken into account.

Relevant code:

import tkinter as tk
from tkinter import ttk

class ZombieSelector(tk.LabelFrame):
    def __init__(self,
                 master,
                 options: list=[],
                 max: int=10,
                 cnf={},
                 **kwargs) -> None:
        kwargs = cnf or kwargs

        dropDown = ttk.Combobox(master, state="readonly", values=options, width=15)
        dropDown.bind("<<ComboboxSelected>>", self.__addZombie)

        super().__init__(master, labelwidget=dropDown, width=184, height=max*20+42, **kwargs)

        ttk.Label(self, text="Type").grid(row=0, column=0)
        ttk.Label(self, text="Alive").grid(row=0, column=1)
        ttk.Label(self, text="Dead").grid(row=0, column=2)
wispy heronBOT
#

@quasi needle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

carmine breach
#

try moving the super call to before you make the combobox

#

do you want the combobox inside of master or inside the label frame?

#

in which case, it should be ttk.Combobox(self, ...)

quasi needle
#

I want the combobox as the label for the label frame

carmine breach
#

oh I didn't even know that was possible

quasi needle
#

I've had it working with setting the master of both the combobox and the labelframe to the same widget before I tried creating this child class:

class FiftyPercent(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.iconbitmap("assets/icon.ico")
        self.title("50% Rule")
        self.resizable(False, False)

        self.__toolbar = stk.ToolBar(self, tearoff=0)
        self.__toolbar.add_cascade("file", menukw={"tearoff": 0}, cascadekw={"label": "File"})
        self.__toolbar.file.add_command(label="Close", command=lambda: self.destroy()) # type: ignore
        self.__toolbar.file.add_command(label="Exit", command=lambda: exit()) # type: ignore
        self.config(menu=self.__toolbar)

        with open("zombieHealth.json", "r") as file:
            jsondict = json.load(file)

        ttk.Label(self, text="Zombies").grid(row=0, column=0)

        self.__zombiesDropDown = ttk.Combobox(self, state="readonly", values=list(jsondict.keys()), width=15)
        self.__zombiesDropDown.bind("<<ComboboxSelected>>", self.__addZombie)
        self.__zombiesList = tk.LabelFrame(self, labelwidget=self.__zombiesDropDown)
        self.__zombiesList.grid(row=1, column=0, rowspan=10, padx="2p", pady="2p", ipadx="1p", ipady="1p")
carmine breach
#

is that part working correctly, but it's just the width/height that's a problem?

quasi needle
#

just the width and height

carmine breach
#

ahh ok

quasi needle
#

Old method on the left, new on the right

carmine breach
#

seems that labelframes will only size up to whatever their contents are

quasi needle
#

however, this seems to work:

tk.LabelFrame(self, labelwidget=ttk.Combobox(self), width=184, height=8*20+42).grid(row=1, column=1, padx="2p", pady="2p", ipadx="1p", ipady="1p")
carmine breach
#

yeah this seems like more of a pack/grid issue than a labelframe issue

quasi needle
#

both methods are using the same .grid args

carmine breach
#

hmm I'm really not sure then. This is the extent of my tkinter layout knowledge, haha

quasi needle
#

lol, np, hopefully someone else has an idea

#

it doesnt help that the most comprehensive docs ive found for tkinter were last updated in 2014

quasi needle
wispy heronBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.