英文:
Why is my tkinter scrollbar is not showing up on my text widget?
问题
我正在创建一个餐厅菜单,使用文本小部件来显示我的菜单项。我想在它的最右边添加一个垂直滚动条,但它在我的当前代码中没有显示出来。我该如何修复这个问题?
# 菜单项的文本小部件
scrollbar = tk.Scrollbar(orient='vertical', command=tk.YView, bg='white')
scrollbar.grid(column=0, row=2,)
items = tk.Text(bg='black', font=('Baskerville', 18), wrap=tk.WORD, yscrollcommand=scrollbar.set, height=20)
items.grid(column=0, row=2, columnspan=4, padx=20, pady=20)
scrollbar.config(command=items.yview)
# 标题和按钮
head = tk.Label(text="Menu", font=("Garamond", 40), bg='black')
head.grid(column=0, row=0, columnspan=4, sticky='N', pady=10)
app = tk.Button(text="Entrees", width=12, height=2, command=lambda: printItems("Entree"))
main = tk.Button(text="Mains", width=12, height=2, command=lambda: printItems("Main"))
des = tk.Button(text="Desserts", width=12, height=2, command=lambda: printItems("Dessert"))
deal = tk.Button(text="Configure", width=12, height=2, command=lambda: editWindow())
英文:
I am creating a restaurant menu with a text widget to display my menu items. I want a vertical scrollbar on it's far right but it isn't showing up with my current code. How do I go about fixing this?
# Text Widget For Menu Items
scrollbar = tk.Scrollbar( orient='vertical',command = tk.YView,bg = 'white')
scrollbar.grid(column = 0,row = 2,)
items = tk.Text(bg = 'black',font = ('Baskerville', 18),wrap = tk.WORD,yscrollcommand = scrollbar.set,height = 20)
items.grid(column = 0,row = 2, columnspan = 4,padx = 20,pady = 20)
scrollbar.config( command = items.yview )
# Title And Buttons
head = tk.Label(text = "Menu",font = ("Garamond",40),bg = 'black')
head.grid(column = 0,row = 0,columnspan = 4, sticky = 'N', pady = 10)
app = tk.Button(text="Entrees",width = 12,height = 2,command = lambda: printItems("Entree"))
main = tk.Button(text="Mains",width = 12,height = 2,command = lambda: printItems("Main"))
des = tk.Button(text="Desserts",width = 12,height = 2,command = lambda: printItems("Dessert"))
deal = tk.Button(text="Configure",width = 12,height = 2,command = lambda: editWindow())
答案1
得分: 1
你已将滚动条和文本小部件都放在第0列。如果你想要滚动条在右边,请将其放在第5列,因为文本小部件跨越了4列。在添加滚动条时,你还需要使用sticky
属性,以便它填充分配给它的空间。
scrollbar.grid(column=5, row=2, sticky="ns")
英文:
You've put both the scrollbar and text widget in column 0. If you want the scrollbar on the right, put it in column 5 since the text widget spans 4 columns. You'll also want to use the sticky
attribute when adding the scrollbar so it fills the space allocated to it.
scrollbar.grid(column = 5,row = 2, sticky="ns")
答案2
得分: 1
正如其他人所说,您已经将Scrollbar
放在第0列,这与Text
小部件重叠/覆盖了。
我建议将Text
和Scrollbar
小部件都放在一个框架中,因为这样更容易独立于其他按钮和标签进行排列:
# 创建一个占据4列的框架,以与按钮对齐
frame = tk.Frame(root, bg="black")
frame.grid(row=2, column=0, columnspan=4, padx=20, pady=20)
# 将滚动条放在框架的右侧并垂直填充
scrollbar = tk.Scrollbar(frame, orient='vertical', command=tk.YView, bg='white')
scrollbar.pack(side=tk.RIGHT, fill='y')
# 将文本小部件放在框架的左侧
items = tk.Text(frame, bg='black', font=('Baskerville', 18), wrap=tk.WORD,
yscrollcommand=scrollbar.set, height=20)
items.pack(side=tk.LEFT)
scrollbar.config(command=items.yview)
并且输出结果:
英文:
As the others said, you have put the Scrollbar
in column 0 which is overlapped/covered by the Text
widget.
I would suggest to put both the Text
and Scrollbar
widgets in a frame because it is more easier to arrange them independent of the other buttons and label:
# create a frame that occupies 4 columns to align with the buttons
frame = tk.Frame(root, bg="black")
frame.grid(row=2, column=0, columnspan=4, padx=20, pady=20)
# scrollbar put in right side of the frame and fill vertically
scrollbar = tk.Scrollbar(frame, orient='vertical', command=tk.YView, bg='white')
scrollbar.pack(side=tk.RIGHT, fill='y')
# Text widget put in the left side of the frame
items = tk.Text(frame, bg='black', font=('Baskerville', 18), wrap=tk.WORD,
yscrollcommand=scrollbar.set, height=20)
items.pack(side=tk.LEFT)
scrollbar.config(command=items.yview)
And the output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论