Tkinter滚动条无法滚动

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

Tkinter scrollbar is not scrolling

问题

作为我正在使用Python 3.8构建的tkinter应用程序的一部分,我需要使Notebook中的特定选项卡可滚动。Notebook需要保持固定大小,但问题在于有些情况下选项卡的内容将超出Notebook的大小。

滚动条显示正常,但滚动似乎对选项卡的内容没有影响。看起来它认为正在滚动某些东西,但我不知道是什么。以下是一个带有无效滚动条的选项卡的示例:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

tabs = ttk.Notebook(root, width=200, height=650)

tab_options = tk.Frame(tabs)
tabs.add(tab_options, text="Options")

main_frame = tk.Frame(tab_options)
main_frame.pack()

canvas = tk.Canvas(main_frame)
canvas.pack(side="left", fill="both", expand=1)

scrollbar = ttk.Scrollbar(main_frame, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y", expand=1)

lf_options = tk.Frame(canvas)
lf_options.pack()

canvas.configure(yscrollcommand=scrollbar.set)
canvas.configure(scrollregion=(0, 0, 200, 1000))

for i in range(50):
    ttk.Label(lf_options, text=str(i)).pack()

tabs.pack()

root.mainloop()

我猜想与如何将框架连接到画布有关,但无论如何我都无法使其工作。我看到了一些建议,建议将scrollregion设置为 canvas.bbox("all"),但我不明白如何将其与可以显示的最大高度关联,即Notebook本身的高度。将其用作scrollregion也只会使滚动条无法滚动。

我知道在这里有许多类似的问题,但我没有找到任何一个示例在这种情况下有效。希望能提供一些建议。谢谢!

英文:

As part of a tkinter app I'm building using Python 3.8, I need a particular tab in a Notebook to be scrollable. The notebook needs to remain at a fixed size, but the problem is that there will be cases in which the contents of the tab will exceed the size of the notebook.

The scrollbar appears as it should, but scrolling appears to have no effect on the contents of the tab. It looks like it thinks it's scrolling something but I do not know what. Here's an isolated example of a tab with a scrollbar which has no effect:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

tabs = ttk.Notebook(root, width=200, height=650)

tab_options = tk.Frame(tabs)
tabs.add(tab_options, text="Options")


main_frame = tk.Frame(tab_options)
main_frame.pack()

canvas = tk.Canvas(main_frame)
canvas.pack(side="left",fill="both",expand=1)

scrollbar = ttk.Scrollbar(main_frame,orient="vertical",command=canvas.yview)
scrollbar.pack(side="right", fill="y",expand=1)

lf_options = tk.Frame(canvas)
lf_options.pack()

canvas.configure(yscrollcommand=scrollbar.set)
canvas.configure(scrollregion=(0,0,200,1000))

for i in range(50):
    ttk.Label(lf_options, text=str(i)).pack()

tabs.pack()

root.mainloop()

I imagine it's something to do with how I'm hooking up the frames to the canvas but I cannot for the life of me get it to work. I've seen suggestions about setting scrollregion to

canvas.bbox("all")

but I don't understand how to associate that with the maximum height that can be displayed, i.e. the height of the notebook itself. Using that as the scrollregion also just makes the scrollbar unscrollable.

I know there are many similar questions on here, but I have not found any of those examples to work in this case.

Any advice would be greatly appreciated. Thanks!

答案1

得分: 1

似乎更合逻辑地使用`tk.Listbox`来实现这个目的,查看下面的示例,这是您代码的编辑版本。在这里,滚动条正常工作!

```python
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

tabs = ttk.Notebook(root, width=200, height=650)

tab_options = tk.Frame(tabs)
tabs.add(tab_options, text="Options")

listbox = tk.Listbox(tab_options)
for i in range(50):
    listbox.insert(tk.END, f"Number {i}")
listbox.pack(side="left", fill="both", expand=1)

scrollbar = ttk.Scrollbar(tab_options, orient="vertical", command=listbox.yview)
scrollbar.pack(side="right", fill="y", expand=1)

listbox.configure(yscrollcommand=scrollbar.set)

tabs.pack()

root.mainloop()
英文:

It seems more logical to use a tk.Listbox for this purpose, see below for example an edited version of your code. Here the scrollbar works just as expected!

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

tabs = ttk.Notebook(root, width=200, height=650)

tab_options = tk.Frame(tabs)
tabs.add(tab_options, text="Options")

listbox = tk.Listbox(tab_options)
for i in range(50):
    listbox.insert(tk.END, f"Number {i}")
listbox.pack(side="left", fill="both", expand=1)

scrollbar = ttk.Scrollbar(tab_options, orient="vertical", command=listbox.yview)
scrollbar.pack(side="right", fill="y", expand=1)

listbox.configure(yscrollcommand=scrollbar.set)

tabs.pack()

root.mainloop()

huangapple
  • 本文由 发表于 2023年2月9日 02:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390087.html
匿名

发表评论

匿名网友

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

确定