-1

This is my first time using tkinter and I already did some research on pack and grid. How do I fix this code so that the pack and grid components don't intertwine?

I want to use grid for my checkbox so that 16 checkboxes show up in a column next to the words corresponding to them. Can I do this with pack?

# tkinter will help us with the GUI
import tkinter as tk
from tkinter import filedialog, Text
import os


def data():
    categoriesArray = ["16 words here"] 

    for i in range(16):
        checkbox = tk.Checkbutton(buttonFrame, bg="white")
        checkbox.grid(row=i, column=0, sticky="w")
        tk.Label(canvasFrame, text=categoriesArray[i]).grid(row=i, column=1, sticky="ew")

# Define the scrolling function for the scrollbar
def scrollFunction(event):
    canvas.configure(scrollregion=canvas.bbox("all"), width=200, height=500)

# The root holds the whole app structure. Always attach to root.
root = tk.Tk()

# These two lines literally make the rectangular structure of the app.
canvas = tk.Canvas(root, height = 500, width= 1300, bg="#00008B")
canvas.pack()

# These two lines make the white screen you see on the left of the buttons.
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.03, rely=0.1)

# This is the frame for the buttons on the right
buttonFrame = tk.Frame(root, bg="white")
buttonFrame.place(relwidth=0.13, relheight=0.8, relx=0.85, rely=0.1)

# You need a canvas to define a scrollbar within the app.
# Resource: https://stackoverflow.com/questions/16188420/tkinter-scrollbar-for-frame
canvas=tk.Canvas(buttonFrame)
canvasFrame=tk.Frame(canvas)
scrollbar=tk.Scrollbar(buttonFrame, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)

scrollbar.pack(side="right", fill="y")
canvas.pack(side="left")
canvas.create_window((36,0), window=canvasFrame, anchor='nw')
canvasFrame.bind("<Configure>", scrollFunction)

# Call the data for the categories to show on the right
data()

# This runs the mainframe to work
root.mainloop()

Please let me know anything I can do to make my question better.

Places I've looked but gotten confused: fix this code 'cannot use geometry manager grid inside . which already has slaves managed by pack'

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You could just use grid and not have the problem. Grid can do everything you will likely need except for a few cases where `place` might be used. – Mike - SMT Mar 05 '20 at 18:29

1 Answers1

0

I fixed it. checkbox = tk.Checkbutton(buttonFrame, bg="white") should have canvasFrame instead of buttonFrame.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79