Home > Software design >  How can I dock a combobox inside labelframe1 that is already inside a class?
How can I dock a combobox inside labelframe1 that is already inside a class?

Time:12-20

I have a LabelFrame inside a class. The LabelFrame is anchored in the class, inside def __init __, via "self". Inside the LabelFrame there are various widgets, but anchored only to "self".

I would like to anchor the widgets inside the LabelFrame, so that when I move the position of the LabelFrame I don't have to move each widget individually yet.

How can I dock the combobox inside labelframe1? (always remaining inside the page with the class?). So that by moving the position of the LabelFrame I still don't have to move each widget individually

root = tk.Toplevel()
root.geometry("1200x1000")
root.state("normal")

     class Page (tk.Frame):
         def __init __ (self, master, ** kw):
             super () .__ init __ (master, ** kw)
    
             #labelframe anchored to "self"
             labelframe1 = LabelFrame (self, text = "Label Frame", width = 600, height = 190, bg = "white", foreground = 'black')
             labelframe1.place (x = 10, y = 13)
    
             #combobox anchored to "self"
             asas = Label (self, text = "Name", bg = "black", foreground = 'black', background = 'white', font = 'TkDefaultFont 11')
             asas.place (x = 17, y = 37)
             Name = Entry (self, width = 5)
             Name.place (x = 522, y = 36)

root.mainloop()

CodePudding user response:

Here is your code with a few modifications that place all objects inside labelframe1.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

class Page(tk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        # labelframe anchored to "self.labelframe1"
        self.labelframe1 = tk.LabelFrame(
            self, text = "Label Frame", foreground = 'black')
        self.labelframe1.place(x = 10, y = 13, width = 600, height = 400)

        # combobox anchored to "self.labelframe1"
        self.asas = tk.Label(
            self.labelframe1, text = "Name", background = 'black',
            foreground = 'white', font = 'TkDefaultFont 11')
        self.asas.place(x = 17, y = 36, width = 100, height = 30)

        self.Name = tk.Entry(self.labelframe1, width = 10)
        self.Name.place(x = 122, y = 36)
        # manage the Frame
        self.place(x = 0, y = 0, width = 700, height = 500)

A = Page(root)

# Insert any tkinter widget except(Tk|Toplevel)
A.combo = ttk.Combobox(A.labelframe1, width = 27)
A.combo.place(x = 200, y = 36)

root.geometry('620x425')
root.mainloop()

You can now change labelframe1.place() and objects inside it will move with it.

  • Related