Python Tkinter Grid 列宽不需要扩展第一行不可 Scorallable

问题描述 投票:0回答:1

我有带滚动条的三列,它需要扩展,并且应该拉伸窗口大小,但我无法根据屏幕大小增加列宽,我需要顶部第一行应该需要稳定,就像标题一样。代码如下

import tkinter as tk
from tkinter import *
from tkinter import ttk

root = tk.Tk()
root.title("TEST")
root.geometry("800x600")

frame=ttk.Frame(root)
frame.pack(expand=1, fill=BOTH)

canvas = tk.Canvas(frame)
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)

frame3s = ttk.Frame(canvas)
frame3s.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

RCnt = 0
DataRow = {}
LabCnt = 0;
for gh in range(40):
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"),cursor="hand2", justify=tk.CENTER,relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=0, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"),cursor="hand2", justify=tk.CENTER,relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=1, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"),cursor="hand2", justify=tk.CENTER,relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=2, sticky='ew')
   LabCnt += 1
   RCnt += 1
   frame3s.columnconfigure(gh, weight=1)
   frame3s.rowconfigure(gh, weight=1)

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

canvas.create_window((0, 0), window=frame3s, anchor="nw")
canvas.grid(row=0, column=0, sticky="nsew")
scrollbar.grid(row=0, column=1, sticky="ns")

def _on_mousewheel(event):
   canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
canvas.bind_all("<MouseWheel>", _on_mousewheel)
while True:
   root.update()

请指导我根据屏幕尺寸实现扩展列宽

python tkinter scrollbar tkinter-canvas
1个回答
0
投票

如果您想扩展列以适应画布宽度,您需要:

  • 保存
    canvas.create_window((0, 0), window=frame3s, ...)
  • 的商品ID
  • 使用
    frame3s
    canvas
     的宽度设置为与 
    <Configure>
     上事件 
    canvas
     的回调中 
    canvas.itemconfig()
  • 的宽度相同
  • 使用
    weight=1
     将第 0 列上的 
    frame3s.columnconfigure()
  • 设置为 2

以下是更新后的代码:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("TEST")
root.geometry("800x600")

frame = ttk.Frame(root)
frame.pack(expand=1, fill=tk.BOTH)

canvas = tk.Canvas(frame, bg='pink')
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=scrollbar.set)

frame3s = ttk.Frame(canvas)
frame3s.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

RCnt = 0
DataRow = {}
LabCnt = 0;
for gh in range(40):
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"), cursor="hand2", justify=tk.CENTER, relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=0, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"), cursor="hand2", justify=tk.CENTER, relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=1, sticky='ew')
   LabCnt += 1
   DataRow[LabCnt] = ttk.Label(frame3s, text=LabCnt, font=("Arial", 16, "bold"), cursor="hand2", justify=tk.CENTER, relief="solid")
   DataRow[LabCnt].grid(row=RCnt, column=2, sticky='ew')
   LabCnt += 1
   RCnt += 1

# expand column 0 to 2 to fill the width of the frame
frame3s.columnconfigure((0,1,2), weight=1)

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

# save the item ID of frame3s
frame_id = canvas.create_window((0, 0), window=frame3s, anchor="nw")

canvas.grid(row=0, column=0, sticky="nsew")
scrollbar.grid(row=0, column=1, sticky="ns")

def _on_mousewheel(event):
   canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")

canvas.bind_all("<MouseWheel>", _on_mousewheel)

# set width of frame3s to the same as that of the canvas
canvas.bind('<Configure>', lambda e: canvas.itemconfig(frame_id, width=e.width))

root.mainloop()  # using .mainloop() instead of while loop
© www.soinside.com 2019 - 2024. All rights reserved.