[英]Tkinter: cannot use geometry manager grid inside .!frame2 which already has slaves managed by pack
This is my first time using tkinter
and I already did some research on pack
and grid
.这是我第一次使用tkinter
,我已经对pack
和grid
进行了一些研究。 How do I fix this code so that the pack
and grid
components don't intertwine?如何修复此代码,以便pack
和grid
组件不会交织在一起?
I want to use grid for my checkbox so that 16 checkboxes show up in a column next to the words corresponding to them.我想为我的复选框使用网格,以便 16 个复选框显示在与其对应的单词旁边的列中。 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' 已经有由包管理的奴隶'
I fixed it.我修好了它。 checkbox = tk.Checkbutton(buttonFrame, bg="white")
should have canvasFrame
instead of buttonFrame
. checkbox = tk.Checkbutton(buttonFrame, bg="white")
应该有canvasFrame
而不是buttonFrame
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.