我刚开始使用tkinter而且我不知道如何让滚动条工作。我在stackoverflow上检查了许多关于tkinter的不同线程,我对我究竟需要做什么感到困惑。
我已经添加了滚动条,但它没有滚动任何内容(注释#******* Scrollbar *******)。我希望它能从内容框架中滚动文本。
已经在#*******词典数据*******。
中插入了lorem ipsumfrom 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()
答案 0 :(得分:5)
您将滚动条绑定到您不打包的列表框。这没有意义 您希望将滚动条绑定到已创建Label小部件的文本。但是,您无法轻松地将其绑定到Label小部件。但是,您可以将滚动条绑定到文本小部件。
如果您将Label设为文本,则无法使用textvariable
属性。您不必在按钮回调中设置textvariable,而是必须删除文本内容并在其中放置新内容。
这是修改后的代码:
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()
答案 1 :(得分:3)
要将滚动条连接到可滚动窗口小部件,您需要执行以下操作:
xscrollcommand
和/或yscrollcommand
属性(例如:文本小部件,列表框或画布)。该值通常是滚动条的set
方法。 command
属性。该值通常是要滚动的窗口小部件的xview
或yview
命令。 例如:
text_widget = tk.Text(...)
vertical_scrollbar = tk.Scrollbar(...)
text_widget.configure(yscrollcommand=vertical_scrollbar.set)
vertical_scrollbar.configure(command=text_widget.yview)
它们必须在两个方向上连接,以便滚动条反映窗口小部件中的更改,并且窗口小部件反映滚动条中的更改。
答案 2 :(得分: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
。