英文:
Using tkinter, when I place my tree and buttons on the canvas why do they disappear?
问题
以下是您的代码:
import tkinter
from tkinter import ttk
from tkinter import *
root = tkinter.Tk()
root.geometry("1500x900")
root.title("Writer")
canvas = tkinter.Canvas(root, borderwidth=10)
frame = tkinter.Frame(canvas)
vsb = tkinter.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((0, 0), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all")))
def treeframe1(rows, df):
tree["columns"] = list(range(1, len(df.columns) + 1))
tree['show'] = 'headings'
tree['height'] = '45'
colnames = df.columns
tree.column(1, width=90, anchor='c')
tree.heading(1, text=colnames[0])
for i in range(2, len(df.columns) + 1):
tree.column(i, width=100, anchor='c')
tree.heading(i, text=colnames[i - 1])
for i in rows:
tree.insert('', 'end', values=i)
tree = ttk.Treeview(root, selectmode='browse', height='45')
tree.place(x=960 + 200 + 00, y=45, width=920)
vsb = ttk.Scrollbar(root, orient="vertical", command=tree.yview)
vsb.place(x=950 + 200 + 00, y=45, height=930)
tree.configure(yscrollcommand=vsb.set)
vsb = ttk.Scrollbar(root, orient="horizontal", command=tree.xview)
vsb.place(x=960 + 200 + 00, y=970, width=960)
tree.configure(xscrollcommand=vsb.set)
b1 = Button(root, text="dd1", width=15)
b1.place(x=650 + 200, y=465 + 200 - 600 + 5)
b2 = Button(root, text="dd2", width=15)
b2.place(x=650 + 200, y=515 + 200 - 600 + 5)
b3 = Button(root, text="dd3", width=15)
b3.place(x=650 + 200, y=565 + 200 - 600 + 5)
root.mainloop()
当我将它们放在画布上(即将"root"替换为"frame")时,我看不到它们。为什么会发生这种情况?
我是 tkinter 新手,如果有人能修改代码使其正常工作,将非常有帮助。
英文:
below are my codes
when I place my tree and buttons on the root I can see them
import tkinter
from tkinter import ttk
from tkinter import *
root = tkinter.Tk()
root.geometry("1500x900")
root.title("Writer")
canvas = tkinter.Canvas(root, borderwidth=10)
frame = tkinter.Frame(canvas)
vsb = tkinter.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((0, 0), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all")))
def treeframe1(rows, df):
tree["columns"] = list(range(1, len(df.columns) + 1))
tree['show'] = 'headings'
tree['height'] = '45'
colnames = df.columns
tree.column(1, width=90, anchor='c')
tree.heading(1, text=colnames[0])
for i in range(2, len(df.columns) + 1):
tree.column(i, width=100, anchor='c')
tree.heading(i, text=colnames[i - 1])
for i in rows:
tree.insert('', 'end', values=i)
tree = ttk.Treeview(root, selectmode='browse', height='45')
tree.place(x=960 + 200 + 00, y=45, width=920)
vsb = ttk.Scrollbar(root, orient="vertical", command=tree.yview)
vsb.place(x=950 + 200 + 00, y=45, height=930)
tree.configure(yscrollcommand=vsb.set)
vsb = ttk.Scrollbar(root, orient="horizontal", command=tree.xview)
vsb.place(x=960 + 200 + 00, y=970, width=960)
tree.configure(xscrollcommand=vsb.set)
b1 = Button(root, text="dd1", width=15)
b1.place(x=650 + 200, y=465 + 200 - 600 + 5)
b2 = Button(root, text="dd2", width=15)
b2.place(x=650 + 200, y=515 + 200 - 600 + 5)
b3 = Button(root, text="dd3", width=15,)
b3.place(x=650 + 200, y=565 + 200 - 600 + 5)
root.mainloop()
but when I place them on the canvas (i.e. replacing the "root" with "frame") i dont see them. Why is that happening?
I am new to tkinter so if someone to modify the code to make it work that would be really helpful
答案1
得分: 1
问题的根本在于您没有为框架指定大小,因此它默认为一个像素。然后,您正在使用 place 将按钮放置在框架中,而 place 不会导致包含小部件(frame)调整大小以适应其子元素。因此,框架保持1像素宽和1像素高。
您可以通过为框架指定足够大的显式宽度和高度来轻松查看这一点。例如,如果您执行 canvas.create_window((0, 0), window=frame, anchor="nw", width=1100, height=1100),按钮将会显示出来。
英文:
The root of the problem is that you haven't given the frame a size, so it defaults to being one pixel. You are then using place to put the buttons in the frame, and place won't cause the containing widget (frame) to resize to fit its children. So, the frame remains 1 pixel wide and 1 pixel tall.
You can easily see this by giving the frame an explicit width and height that is big enough to fill the space. For example, if you do canvas.create_window((0, 0), window=frame, anchor="nw", width=1100, height=1100) the buttons will appear.
答案2
得分: 0
你应该使用 pack 或 grid 而不是 place 来添加这些按钮。
b1 = Button(frame, text="dd1", width=15)
b1.pack(padx=(850, 0), pady=(50, 0))
b2 = Button(frame, text="dd2", width=15)
b2.pack(padx=(850, 0), pady=(50, 0))
b3 = Button(frame, text="dd3", width=15)
b3.pack(padx=(850, 0), pady=(50, 0))
你可以使用 padx 和 pady 来调整按钮在框架上的位置。我添加了任意值以使其在位置上类似于你的 place 值。
英文:
You should use pack or grid instead of place to add the Buttons.
b1 = Button(frame, text="dd1", width=15)
b1.pack(padx=(850, 0), pady=(50, 0))
b2 = Button(frame, text="dd2", width=15)
b2.pack(padx=(850, 0), pady=(50, 0))
b3 = Button(frame, text="dd3", width=15)
b3.pack(padx=(850, 0), pady=(50, 0))
You can use padx and pady to adjust the location of the buttons on the Frame. I added arbitrary values to make it a similar location to your place values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论