我刚开始使用tkinter,但不知道如何使滚动条正常工作。 我在stackoverflow上检查了许多有关tkinter的不同线程,而我究竟需要做什么感到困惑。

我已经添加了滚动条,但是它没有滚动任何内容(注释为#*******滚动条*******)。 我希望它从内容框架滚动文本。

#*****词典数据*******中已经插入了lorem ipsum。

from tkinter import *

class Search(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.minsize(380,444)
        self.maxsize(380,444)
        self.title("")

        self.search_start = StringVar()
        self.search_start.set('Enter your query')
        self.search_result = StringVar()

        self.bind("<Return>", self.search_button)

        self.scrollbar = Frame(self)
        self.scrollbar.pack(side=RIGHT, fill=Y)
        self.search_bar = Frame(self, bg="blue")
        self.search_bar.pack(side=TOP, fill=X)
        self.index = Frame(self)
        self.index.pack(side=LEFT)
        self.content = Frame(self)
        self.content.pack(side=TOP, fill=X)
        self.status_bar = Frame(self, bg="yellow")
        self.status_bar.pack(side=BOTTOM, fill=X)

# ******* Search Input *******

        self.entry = Entry(self.search_bar, textvariable = self.search_start)
        self.entry.pack(side=LEFT, padx=4, pady=4)

# ******* Search Button *******

        self.search = Button(self.search_bar, text="Search", command=self.search_button)
        self.search.pack(side=LEFT)

# ******* Content *******

        self.content = Label(self.content, textvariable = self.search_result, bg="ghost white", wraplength=360, width=52, height=26, justify=LEFT, anchor=NW) # justify == align
        self.content.pack(side=LEFT)

# ******* Scrollbar *******

        self.scrollbar = Scrollbar(self.scrollbar)
        self.scrollbar.pack(side=RIGHT, fill=Y)

        self.listbox = Listbox(self.scrollbar, yscrollcommand=self.scrollbar.set)


        self.scrollbar.config(command=self.listbox.yview)

# ******* Toolbar *******

        self.status_bar = Label(self.status_bar, text="toolbar", bg="yellow", relief=FLAT)
        self.status_bar.pack(side=LEFT)

# ******* Search Button *******

    def search_button(self, event=None):
        result_text = dictionary_data.get(self.entry.get(), "Entry not in database.\n\nCheck if you wrote your query correctly or type index to see all available entries.")
        self.search_result.set(result_text)
        self.search_start.set("")

# ******* Dictionary data *******

dictionary_data = {
    "index": 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin laoreet a enim a elementum. Ut tristique maximus mauris vitae tempus. Aliquam erat volutpat. Etiam semper, libero posuere vulputate aliquet, arcu odio fermentum ex, vulputate elementum urna felis fringilla nisi. Quisque nec nunc eu lacus fermentum sollicitudin quis ut ante. Maecenas consectetur varius eros, quis mollis est tincidunt eget. Morbi tempus aliquam dui, quis placerat metus porta sit amet. Suspendisse vitae diam at nibh semper iaculis quis quis velit. Morbi ac ipsum porttitor, lacinia eros at, volutpat arcu. Vestibulum ut diam convallis, auctor metus a, malesuada ante. Aliquam vitae leo magna. Quisque rutrum tristique neque, ut venenatis nibh viverra eget. Duis vitae felis non lacus viverra varius. Nunc placerat eros sed arcu vestibulum, id pharetra felis fermentum. Quisque semper vitae dolor sed molestie. Suspendisse semper ante eu sem ultricies ullamcorper. Quisque suscipit pellentesque varius. Integer magna lorem, lacinia in dolor quis, commodo tempus nibh. Etiam fermentum cursus vehicula. Vestibulum congue arcu at purus viverra faucibus. Pellentesque pellentesque facilisis massa, vel iaculis mauris egestas eu. Sed ut risus eu turpis sodales faucibus. Morbi aliquam sit amet purus ac rhoncus. Aliquam eros orci, laoreet ut fringilla vitae, sodales vitae ipsum. Nunc eu enim condimentum, fermentum massa hendrerit, cursus orci. Suspendisse potenti. Nulla faucibus velit ipsum, sollicitudin elementum nibh consequat ac. Nunc et condimentum nibh. Sed a malesuada nisl.Nulla at est arcu. Maecenas elementum ex nulla. Aliquam erat volutpat. Vivamus mattis iaculis nulla, eget tempus purus fermentum vel. Etiam auctor dolor eget eros scelerisque accumsan. Donec lacus tellus, interdum non gravida quis, cursus non magna. Sed lacinia, elit quis hendrerit lobortis, lorem dolor viverra purus, interdum vestibulum enim orci ac nibh. Fusce tellus lacus, pharetra at dignissim eu, maximus at ipsum. Fusce nec semper felis.',
}

# ******* Loop *******

app = Search()
app.mainloop()

#1楼 票数:5 已采纳

您正在将滚动条绑定到没有包装的列表框。 那没有道理。
您想将滚动条绑定到已创建标签小部件的文本。 但是,您不能轻松地将其绑定到Label小部件。 但是,您可以将滚动条绑定到“文本”小部件。

如果将标签设置为文本,则不能使用textvariable属性。 代替在按钮回调中设置textvariable,必须删除Text的内容并将新内容放入其中。

这是修改后的代码:

from tkinter import *

class Search(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.minsize(380,444)
        self.maxsize(380,444)
        self.title("")

        self.search_start = StringVar()
        self.search_start.set('Enter your query')
        self.search_result = StringVar()

        self.bind("<Return>", self.search_button)

        self.scrollbar_frame = Frame(self)
        self.scrollbar_frame.pack(side=RIGHT, fill=Y)
        self.search_bar = Frame(self, bg="blue")
        self.search_bar.pack(side=TOP, fill=X)
        self.index = Frame(self)
        self.index.pack(side=LEFT)
        self.content = Frame(self)
        self.content.pack(side=TOP, fill=X)
        self.status_bar = Frame(self, bg="yellow")
        self.status_bar.pack(side=BOTTOM, fill=X)

# ******* Search Input *******

        self.entry = Entry(self.search_bar, textvariable = self.search_start)
        self.entry.pack(side=LEFT, padx=4, pady=4)

# ******* Search Button *******

        self.search = Button(self.search_bar, text="Search", command=self.search_button)
        self.search.pack(side=LEFT)

# ******* Content *******

        self.content = Text(self.content, bg="ghost white", width=52, height=26) # justify == align
        self.content.pack(side=LEFT)

# ******* Scrollbar *******

        self.scrollbar = Scrollbar(self.scrollbar_frame)
        self.scrollbar.pack(side=RIGHT, fill=Y)

        self.content.config(yscrollcommand=self.scrollbar.set)

        self.scrollbar.config(command=self.content.yview)

# ******* Toolbar *******

        self.status_bar = Label(self.status_bar, text="toolbar", bg="yellow", relief=FLAT)
        self.status_bar.pack(side=LEFT)

# ******* Search Button *******

    def search_button(self, event=None):
        result_text = dictionary_data.get(self.entry.get(), "Entry not in database.\n\nCheck if you wrote your query correctly or type index to see all available entries.")
        self.content.delete(1.0, END)
        self.content.insert(END, result_text)
        self.search_start.set("")

# ******* Dictionary data *******

dictionary_data = {
    "index": 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin laoreet a enim a elementum. Ut tristique maximus mauris vitae tempus. Aliquam erat volutpat. Etiam semper, libero posuere vulputate aliquet, arcu odio fermentum ex, vulputate elementum urna felis fringilla nisi. Quisque nec nunc eu lacus fermentum sollicitudin quis ut ante. Maecenas consectetur varius eros, quis mollis est tincidunt eget. Morbi tempus aliquam dui, quis placerat metus porta sit amet. Suspendisse vitae diam at nibh semper iaculis quis quis velit. Morbi ac ipsum porttitor, lacinia eros at, volutpat arcu. Vestibulum ut diam convallis, auctor metus a, malesuada ante. Aliquam vitae leo magna. Quisque rutrum tristique neque, ut venenatis nibh viverra eget. Duis vitae felis non lacus viverra varius. Nunc placerat eros sed arcu vestibulum, id pharetra felis fermentum. Quisque semper vitae dolor sed molestie. Suspendisse semper ante eu sem ultricies ullamcorper. Quisque suscipit pellentesque varius. Integer magna lorem, lacinia in dolor quis, commodo tempus nibh. Etiam fermentum cursus vehicula. Vestibulum congue arcu at purus viverra faucibus. Pellentesque pellentesque facilisis massa, vel iaculis mauris egestas eu. Sed ut risus eu turpis sodales faucibus. Morbi aliquam sit amet purus ac rhoncus. Aliquam eros orci, laoreet ut fringilla vitae, sodales vitae ipsum. Nunc eu enim condimentum, fermentum massa hendrerit, cursus orci. Suspendisse potenti. Nulla faucibus velit ipsum, sollicitudin elementum nibh consequat ac. Nunc et condimentum nibh. Sed a malesuada nisl.Nulla at est arcu. Maecenas elementum ex nulla. Aliquam erat volutpat. Vivamus mattis iaculis nulla, eget tempus purus fermentum vel. Etiam auctor dolor eget eros scelerisque accumsan. Donec lacus tellus, interdum non gravida quis, cursus non magna. Sed lacinia, elit quis hendrerit lobortis, lorem dolor viverra purus, interdum vestibulum enim orci ac nibh. Fusce tellus lacus, pharetra at dignissim eu, maximus at ipsum. Fusce nec semper felis.',
}

# ******* Loop *******

app = Search()
app.mainloop()

#2楼 票数:3

要将滚动条连接到可滚动的小部件,您需要执行以下操作:

  1. 设置要滚动的小部件的xscrollcommand和/或yscrollcommand属性(例如,文本小部件,列表框或画布)。 该值通常是滚动条的set方法。
  2. 设置滚动条的command属性。 该值通常是要滚动的小部件的xviewyview命令。

例如:

text_widget = tk.Text(...)
vertical_scrollbar = tk.Scrollbar(...)

text_widget.configure(yscrollcommand=vertical_scrollbar.set)
vertical_scrollbar.configure(command=text_widget.yview)

它们必须在两个方向上都挂钩,以便滚动条反映小部件中的更改,而小部件反映出滚动条中的更改。

#3楼 票数:1

这是一个工作示例:

from tkinter import *

# ******* Dictionary data *******

dictionary_data = {
    "index": 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin laoreet a enim a elementum. Ut tristique maximus mauris vitae tempus. Aliquam erat volutpat. Etiam semper, libero posuere vulputate aliquet, arcu odio fermentum ex, vulputate elementum urna felis fringilla nisi. Quisque nec nunc eu lacus fermentum sollicitudin quis ut ante. Maecenas consectetur varius eros, quis mollis est tincidunt eget. Morbi tempus aliquam dui, quis placerat metus porta sit amet. Suspendisse vitae diam at nibh semper iaculis quis quis velit. Morbi ac ipsum porttitor, lacinia eros at, volutpat arcu. Vestibulum ut diam convallis, auctor metus a, malesuada ante. Aliquam vitae leo magna. Quisque rutrum tristique neque, ut venenatis nibh viverra eget. Duis vitae felis non lacus viverra varius. Nunc placerat eros sed arcu vestibulum, id pharetra felis fermentum. Quisque semper vitae dolor sed molestie. Suspendisse semper ante eu sem ultricies ullamcorper. Quisque suscipit pellentesque varius. Integer magna lorem, lacinia in dolor quis, commodo tempus nibh. Etiam fermentum cursus vehicula. Vestibulum congue arcu at purus viverra faucibus. Pellentesque pellentesque facilisis massa, vel iaculis mauris egestas eu. Sed ut risus eu turpis sodales faucibus. Morbi aliquam sit amet purus ac rhoncus. Aliquam eros orci, laoreet ut fringilla vitae, sodales vitae ipsum. Nunc eu enim condimentum, fermentum massa hendrerit, cursus orci. Suspendisse potenti. Nulla faucibus velit ipsum, sollicitudin elementum nibh consequat ac. Nunc et condimentum nibh. Sed a malesuada nisl.Nulla at est arcu. Maecenas elementum ex nulla. Aliquam erat volutpat. Vivamus mattis iaculis nulla, eget tempus purus fermentum vel. Etiam auctor dolor eget eros scelerisque accumsan. Donec lacus tellus, interdum non gravida quis, cursus non magna. Sed lacinia, elit quis hendrerit lobortis, lorem dolor viverra purus, interdum vestibulum enim orci ac nibh. Fusce tellus lacus, pharetra at dignissim eu, maximus at ipsum. Fusce nec semper felis.',
}

# ******* Search Button *******

def search_button(event=None):
    content.delete("1.0", END)
    result_text = dictionary_data.get(entry.get(), "Entry not in database.\n\nCheck if you wrote your query correctly or type index to see all available entries.")
    search_result = result_text
    search_start = ""
    entry.delete(0, END)
    content.insert(END, search_result)


master = Tk()

master.minsize(580,444)
master.maxsize(580,444)
master.title("Master")

search_start = "Enter your query"
search_result = ""
master.bind("<Return>", search_button)

# ******* Search Input *******

entry = Entry(master)
entry.insert(END, search_start)
entry.pack(side=LEFT, padx=4, pady=4)

# ******* Search Button *******

search = Button(master, text="Search", command=search_button)
search.pack(side=LEFT)

# ******* Scroll & Content *******

scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
content = Text(master, yscrollcommand=scrollbar.set, bg="ghost white")
content.insert(END, search_result)
content.pack(side=LEFT)
scrollbar.config(command=content.yview)

# ******* Loop ***************

mainloop()

基本上,我使用Text而不是Label

  ask by Gunnm translate from so

未解决问题?本站智能推荐:

1回复

Tkinter滚动条不起作用

我在python 3.4上运行了一段tkinter代码,这是一个放置在带有垂直滚动条的画布中的大框架,但是该滚动条是灰色的,似乎与框架的大小无关。 我使用的代码本质上是: master_frame充满了使用网格几何管理器放置的约36 master_frame部件,并且达到了约2000像
1回复

tkinter:滚动条出现但不起作用

即使出现滚动条并且它是可移动的,它也不会滚动窗口中的文本。 我是tkinter的新手,似乎找不到问题。 这是我尝试过的:
1回复

由于文本框大小,Tkinter 滚动条不起作用

我想在窗口大小内绑定文本框。 但是在我的代码中,文本框垂直超出了窗口大小,这就是滚动条不起作用的原因(据我观察)。 此外,滚动条与框架而不是文本框相连。 我希望它与文本框相连。
1回复

Tkinter滚动条位置,yview_moveto()似乎不起作用

考虑以下示例: 预期的输出: 中间的滚动条。 但是, 无论我给yview_moveto()赋予什么值,我总是将滚动条放在顶部 ,如下所示: 我怎样才能解决这个问题?
1回复

Tkinter-仅在一个窗口中需要垂直滚动条-容器内有多个窗口(框架)

我正在用Python 3.7编写一个tkinter应用程序,但是我以前从未使用过GUI,因此我经验不足。 我想将带有垂直滚动条的画布仅添加到三个窗口之一。 页面通过按钮相互连接-当我按下StartPage上的按钮时,它将带我进入PageOne,这样,我就拥有了可以将我带到不同页面的按钮。
1回复

保存功能在Tkinter窗口中不起作用

我有一个具有保存和另存为功能的Tkinter程序。 我的“另存为”工作正常,但“另存为”却不能。 但是,每当我按下“保存”按钮时,都会引发错误:NameError:未定义名称“ save_as_dialog” 如您所见,该文件在不同的行上存储了不同的信息。 这是程序的关键部分,
1回复

在窗格窗口中使用滚动条调整 Treeview 的大小不起作用

目标: 带有滚动条的树状视图的窗格框架/窗口 调整框架大小应调整 Treeview 的大小并保持滚动条可见 我尝试了以下代码: 滚动条是这样显示的 我还尝试将 Treeview & Scrollbar 安装在单独的框架中(使用网格),这会导致滚动条按预期显示(Treeview
2回复

为什么我的列表框和滚动条不起作用?

我正在尝试制作一个具有滚动条的列表框,但是以下代码似乎未显示在该代码上,请有人能帮助我修复它吗?