提问人:Rookie Mac 提问时间:11/13/2023 更新时间:11/13/2023 访问量:61
tkInter 嵌套画布不显示帧的完整内容
tkInter nested canvas's not display full contents of frames
问:
我正在尝试构建一个约会类型的应用程序。我需要嵌套的滚动条来模拟 Excel“冻结顶行”类型的函数。我可能有太多的列无法方便地容纳,所以我在画布的框架上放置了一个水平滚动条。然后,我可能有太多的行,所以我将垂直滚动条嵌套到另一个画布/框架组合中。水平滚动需要滚动“标题”和详细内容。垂直滚动只需要滚动列中的详细内容。我已经工作了,但我无法获得“完整显示”。在某个地方,其中一个外部框架似乎限制了其他一切。 以下是代码的大纲:
import tkinter as tk
from tkinter import ttk
i1 = 10 #testing number of columns
j1 = 20 #testing number of rows
root = tk.Tk()
full_frame = ttk.Frame(root, border=8, relief="ridge")
full_frame.grid(row=0,column=0)
buttons_frame = ttk.Frame(full_frame, border=8, relief="ridge")
buttons_frame.grid(row = 0, column = 0, sticky="w")
#user control buttons will go here
l = ttk.Label(buttons_frame, text = "buttons frame ")
l.grid (row=0, column = 0)
#build horizontally scrollable frame
dispatch_frame = ttk.Frame(full_frame,border=8, relief="ridge")
dispatch_frame.grid(row=1, column=0, sticky="w")
h_canvas = tk.Canvas(dispatch_frame)
h_canvas.grid(row=0,column=0)
h_scrollbar = ttk.Scrollbar(dispatch_frame, orient="horizontal", command=h_canvas.xview)
h_scrollbar.grid(row=2, column = 0, columnspan = 1, sticky="s,e,w")
horiz_frame = ttk.Frame(h_canvas,border=8, relief="ridge")
horiz_frame.grid(row=0,column=0)
horiz_frame.bind("<Configure>", lambda e: h_canvas.configure(scrollregion=h_canvas.bbox("all")))
h_canvas.create_window((0, 0), window=horiz_frame, anchor="nw")
h_canvas.configure(xscrollcommand=h_scrollbar.set)
#add techs (column titles)
tech_frame=ttk.Frame(horiz_frame, border=8, relief="ridge")
tech_frame.grid(row=0,column=0)
for i in range(i1):
l = ttk.Label(tech_frame, text = "tech "+str(i), width=10)
l.grid (row=1, column = i)
#build job frame
job_frame=ttk.Frame(horiz_frame, border=8, relief="ridge", width=horiz_frame.winfo_reqheight())
job_frame.grid(row=1, column=0, sticky="e,w")
v_canvas = tk.Canvas(job_frame)
v_canvas.grid(row=0,column=0)
v_scrollbar = ttk.Scrollbar(job_frame, orient="vertical", command=v_canvas.yview)
v_scrollbar.grid(row = 0, column = 1, rowspan = 1 , sticky="e,n,s")
v_frame=ttk.Frame(v_canvas,border=8, relief="ridge", width = 10 * i1)
v_frame.grid(row=0,column=0, sticky="e,w")
v_frame.bind("<Configure>", lambda e: v_canvas.configure(scrollregion=v_canvas.bbox("all")))
v_canvas.create_window((0, 0), window=v_frame, anchor="nw")
v_canvas.configure(yscrollcommand=v_scrollbar.set)
#populate rows for each column
for i in range(i1):
for j in range(j1):
l1 = ttk.Label(v_frame, text = " Row: " + str(j), width=10)
l1.grid(row=j, column = i)
root.mainloop()
我已经自学python几个月了,这个让我卡住了。 感谢您的任何建议。
我希望有足够大的外部框架/窗口来容纳所有控件和至少一些数据。 水平似乎工作正常,但我看不到每行的最后几列和最后几行。(它们隐藏在窗户/框架下方)。
答: 暂无答案
评论