如何去除框架和滚动条之间的间隙?

huangapple go评论66阅读模式
英文:

How to remove gap between a frame and its scrollbar?

问题

我有三个框架:left_frame、middle_frame和right_frame。我想让left_frame和它的滚动条之间没有间隙。我该如何实现?

import tkinter as tk
from tkinter import ttk

def create_dummy_chart(frame, index):
    # 创建一个虚拟图表
    label = tk.Label(frame, text=f"Chart {index}", width=10, height=2, relief="solid")
    label.pack(side="top", pady=5)

# 创建主窗口
window = tk.Tk()
window.title("Chart Display")
window.geometry("800x300")

# 创建左侧框架
left_frame = tk.Frame(window, width=200, height=300, relief="solid", bd=1)
left_frame.pack(side="left", fill="y")

# 创建一个包含left_frame和滚动条的画布
canvas = tk.Canvas(left_frame, width=200, height=300)
canvas.pack(side="left", fill="y")

# 创建滚动条
scrollbar = ttk.Scrollbar(left_frame, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")

# 配置画布以使用滚动条
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))

# 在画布内创建一个框架来容纳图表
chart_frame = tk.Frame(canvas)

# 将框架添加到画布
canvas.create_window((0, 0), window=chart_frame, anchor="nw")

# 在chart_frame中创建虚拟图表
num_charts = 200
for i in range(num_charts):
    create_dummy_chart(chart_frame, i)

# 更新画布可滚动区域
chart_frame.update_idletasks()
canvas.configure(scrollregion=canvas.bbox("all"))

# 创建中间框架
middle_frame = tk.Frame(window, width=200, height=300, relief="solid", bd=1)
middle_frame.pack(side="left", fill="y")

# 创建右侧框架
right_frame = tk.Frame(window, width=400, height=300, relief="solid", bd=1)
right_frame.pack(side="left", fill="both", expand=True)

# 运行Tkinter事件循环
window.mainloop()

我有left_frame和垂直滚动条。但是,它们之间有间隙。我期望它们之间没有间隙。我该如何实现这一点?

我有的情况:

如何去除框架和滚动条之间的间隙?

我期望的情况:

如何去除框架和滚动条之间的间隙?

英文:

I have three frames; left_frame, middle_frame and right_frame. I would like to have no gap between left_frame and its scrollbar. How can I achieve it?

import tkinter as tk
from tkinter import ttk

def create_dummy_chart(frame, index):
    # Create a dummy chart
    label = tk.Label(frame, text=f&quot;Chart {index}&quot;, width=10, height=2, relief=&quot;solid&quot;)
    label.pack(side=&quot;top&quot;, pady=5)

# Create the main window
window = tk.Tk()
window.title(&quot;Chart Display&quot;)
window.geometry(&quot;800x300&quot;)

# Create the left frame
left_frame = tk.Frame(window, width=200, height=300, relief=&quot;solid&quot;, bd=1)
left_frame.pack(side=&quot;left&quot;, fill=&quot;y&quot;)

# Create a canvas to hold the left_frame and the scrollbar
canvas = tk.Canvas(left_frame, width=200, height=300)
canvas.pack(side=&quot;left&quot;, fill=&quot;y&quot;)

# Create the scrollbar
scrollbar = ttk.Scrollbar(left_frame, orient=&quot;vertical&quot;, command=canvas.yview)
scrollbar.pack(side=&quot;right&quot;, fill=&quot;y&quot;)

# Configure the canvas to use the scrollbar
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind(&quot;&lt;Configure&gt;&quot;, lambda e: canvas.configure(scrollregion=canvas.bbox(&quot;all&quot;)))

# Create a frame inside the canvas to hold the charts
chart_frame = tk.Frame(canvas)

# Add the frame to the canvas
canvas.create_window((0, 0), window=chart_frame, anchor=&quot;nw&quot;)

# Create dummy charts in the chart_frame
num_charts = 200
for i in range(num_charts):
    create_dummy_chart(chart_frame, i)

# Update the canvas scrollable area
chart_frame.update_idletasks()
canvas.configure(scrollregion=canvas.bbox(&quot;all&quot;))

# Create the middle frame
middle_frame = tk.Frame(window, width=200, height=300, relief=&quot;solid&quot;, bd=1)
middle_frame.pack(side=&quot;left&quot;, fill=&quot;y&quot;)

# Create the right frame
right_frame = tk.Frame(window, width=400, height=300, relief=&quot;solid&quot;, bd=1)
right_frame.pack(side=&quot;left&quot;, fill=&quot;both&quot;, expand=True)

# Run the Tkinter event loop
window.mainloop()

I have left_frame and vertical scrollabr. But, I ended up having a gap between them. I expect to have no gap between them. How can I achieve this?

What I have:

如何去除框架和滚动条之间的间隙?

What I expect:

如何去除框架和滚动条之间的间隙?

答案1

得分: 1

如果您指的是画布和滚动条之间的一个或两个像素的空白,您可以将画布的highlightthickness属性设置为零,或者将highlightcolor设置为与画布背景相同的颜色,以便在画布具有键盘焦点时不可见高亮环。

您可能还需要在打包画布和滚动条时明确将padx值设置为零。在某些平台上,它可能默认为1。

如果您指的是画布比画布上的项目更宽,那么您可以设置一个绑定来始终将画布的宽度与画布上项目的宽度匹配。

您可以这样做:

chart_frame.bind("<Configure>", lambda event: canvas.configure(width=chart_frame.winfo_width()))
英文:

If you're referring to the one or two pixel space between the canvas and the scrollbar, you can set the highlightthickness attribute of the canvas to zero, or you can set the highlightcolor to the same color as the background of the canvas so that the highlight ring is not visible when the canvas has the keyboard focus.

You might also need to explicitly set the padx value to zero when packing the canvas and scrollbar. On some platforms it might default to 1.

If you're referring to the fact that the canvas is wider than the items in the canvas, then you can set up a binding to set the width of the canvas to always match the width of the items on the canvas.

You can do that like this:

chart_frame.bind(&quot;&lt;Configure&gt;&quot;, lambda event: canvas.configure(width=chart_frame.winfo_width()))

huangapple
  • 本文由 发表于 2023年6月1日 22:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定