0

I am a novice Programmer learning GUI and Tkinter for the first time, attempting to make a productivity app for myself. I am trying not to use AI as much as possible.

I have created a Frame within the root window, and in it I am attempting to place a Label, and two Buttons.

The problem that I am trying to solve is that it appears the Label is on top of both of the Buttons, and if I make the label take up more than 1 line or row, then the buttons below no longer fit.

GUI

I have attempted to add this code:

self.bottom_info_label.grid(row=0, column=0)
self.move_button.grid(row=0, column=1, sticky="e")
self.new_project_button.grid(row=1, column=1, sticky="e")

But it appears to overwrite the pack method above where I've anchored to buttons to the right. So I've removed the .grid code for now

Here is my code for this Frame:

# Bottom Frame
self.bottom_frame = tk.Frame(
    self.root,
    borderwidth=l,
    relief="solid",
    padx=8,
    pady=4,
    height=80
)
self.bottom_frame.pack(side="bottom", fill="x")
self.bottom_frame.pack_propagate(False)

# Set Bottom Frame details/information
self.bottom_info_label = tk.Label(
    self.bottom_frame,
    textvariable=self.bottom_label_text,
    font=("Aria1", 9, "italic")
self.bottom_info_label.pack(anchor="w")

# Bindings
self.active_list_box.bind("<<ListboxSe1ect>>", self.on_select_event)
self.hold_list_box.bind("<<ListboxSe1ect>>", self.on_select_event)

# Test:    self.root.bind("<ButtonPress-1>", self.deselect_item)

# Move Project Button
self.move_button = tk.Button(
    self.bottom_frame,
    text="Move to Projects on Hold",
    state="disabled",
    command=self.move_selected
)
self.move_button.pack(anchor="e", pady=(0, 0))

# New Project Button
self.new_project_button = tk.Button(
    self.bottom_frame,
    text="New Project...",
    command=self.open_new_project_dialog
)
self.new_project_button.pack(anchor="e", pady=(0, 0))
New contributor
green_bloodshed is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    You need to supply a working example. Commented 2 days ago
  • what is your expected result? Commented 2 days ago
  • 1
    you can't use pack and grid at the same time in one Frame. You may have to add two frames using grid - left_frame, right_frame - and inside left_frame add Label` (using pack), and inside rigth_frame add Buttons (using pack) Commented 2 days ago
  • Take a look at this Tkinter Tutorial, which offers some excellent guidance on all Tkinter widgets. Commented yesterday

1 Answer 1

0

I'm not sure what result you expect.

You can't use pack and grid at the same time in one Frame.

You may have to add two frames using grid - left_frame, right_frame - and inside left_frame add Label using pack, and inside rigth_frame add Buttons using pack

Grid may need to use weight to use all free space in cell with Label

bottom_frame.columnconfigure(0, weight=1)
bottom_frame.rowconfigure(0, weight=1)

Minimal working code.

I add background to better shows what area has objects.

import tkinter as tk

root = tk.Tk()
# root.geometry("500x200")

# -- top ---
top_frame = tk.Frame(root)
top_frame.pack(side="top", fill="both")

# ---

top_text = tk.Text(top_frame, bg="white", height=5)
top_text.pack(fill="both", expand=True, padx=5, pady=5)

# --- bottom ---

bottom_frame = tk.Frame(root)
bottom_frame.pack(side="bottom", fill="x")

bottom_frame.columnconfigure(0, weight=1)
bottom_frame.rowconfigure(0, weight=1)

# ---

left_frame = tk.Frame(bottom_frame)
left_frame.grid(row=0, column=0, sticky="news")

label_1 = tk.Label(left_frame, text="Label #1", bg="red")
label_1.pack(fill="both", expand=True)
# label_1.pack(anchor="nw")

# label_2 = tk.Label(left_frame, text="Label #2", bg="yellow")
# label_2.pack(fill="x")

# text = tk.Text(left_frame, bg="gray", height=3)
# text.pack(fill="x")
# text.insert("end", "Text #1: Hello World!\n")
# text.insert("end", "Text #2: Hello World!\n")
# text.insert("end", "Text #3: Hello World!\n")

# ---

right_frame = tk.Frame(bottom_frame)
right_frame.grid(row=0, column=1, sticky="swe")

button_1 = tk.Button(right_frame, text="Button #1", bg="green")
button_1.pack()

button_2 = tk.Button(right_frame, text="Button #2", bg="blue")
button_2.pack()

# button_3 = tk.Button(right_frame, text="Button #3", bg="magenta")
# button_3.pack()

# ---

root.mainloop()

Result:

label_1.pack(fill="both", expand=True)

enter image description here

label_1.pack(anchor="nw")

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.