我想在tkinter窗口中添加2个滚动条。
这是我设法得到的:
from tkinter import *
from tkinter import ttk
options = ["A", "B", "C", "D", "E", "F", "G", "H"]
root = Tk()
root.title("random software")
content = ttk.Frame(root)
content.pack(fill=BOTH, expand=1);
scrollable_canvas = Canvas(content)
scrollable_canvas.pack(side=LEFT, fill=BOTH, expand=1)
vscrollbar = ttk.Scrollbar(content, orient=VERTICAL, command=scrollable_canvas.yview)
vscrollbar.pack(side=RIGHT, fill=Y)
hscrollbar = ttk.Scrollbar(content, orient=HORIZONTAL, command=scrollable_canvas.xview)
hscrollbar.pack(side=BOTTOM, fill=X)
scrollable_canvas.configure(yscrollcommand=vscrollbar.set)
scrollable_canvas.configure(xscrollcommand=hscrollbar.set)
scrollable_canvas.bind('<Configure>', lambda e: scrollable_canvas.configure(scrollregion = scrollable_canvas.bbox("all")))
inner_frame = Frame(scrollable_canvas)
scrollable_canvas.create_window((0, 0), window=inner_frame, anchor="nw")
if hscrollbar:
print("hey")
A_label = Label(inner_frame, text = "A")
A_entry = Entry(inner_frame, width = 5)
A_hint = Label(inner_frame, text = "lorem")
A_hint2 = Label(inner_frame, text = "lorem")
A_hint3 = Label(inner_frame, text = "lorem")
A_hint4 = Label(inner_frame, text = "lorem")
A_hint5 = Label(inner_frame, text = "lorem")
B_label = Label(inner_frame, text = "B")
B_entry = Entry(inner_frame, width = 5)
B_hint = Label(inner_frame, text = "ipsum")
C_label = Label(inner_frame, text = "C")
C_entry = Entry(inner_frame, width = 5)
C_hint = Label(inner_frame, text = "dolor")
D_label = Label(inner_frame, text = "D")
D_entry = Entry(inner_frame, width = 5)
D_hint = Label(inner_frame, text = "sit")
E_label = Label(inner_frame, text = "E")
E_entry = Entry(inner_frame, width = 5)
E_hint = Label(inner_frame, text = "amet")
F_label = Label(inner_frame, text = "F")
F_entry = Entry(inner_frame, width = 5)
F_hint = Label(inner_frame, text = "consectetur")
G_label = Label(inner_frame, text = "G")
G_entry = Entry(inner_frame, width = 5)
G_hint = Label(inner_frame, text = "adipiscing")
H_label = Label(inner_frame, text = "H")
H_entry = Entry(inner_frame, width = 5)
H_hint = Label(inner_frame, text = "elit")
A_label.grid(row = 6, column = 0)
A_entry.grid(row = 6, column = 1)
A_hint.grid(row = 6, column = 3, sticky = "w")
A_hint2.grid(row = 6, column = 4, sticky = "w")
A_hint3.grid(row = 6, column = 5, sticky = "w")
A_hint4.grid(row = 6, column = 6, sticky = "w")
A_hint5.grid(row = 6, column = 7, sticky = "w")
B_label.grid(row = 7, column = 0)
B_entry.grid(row = 7, column = 1)
B_hint.grid(row = 7, column = 3, sticky = "w")
C_label.grid(row = 8, column = 0)
C_entry.grid(row = 8, column = 1)
C_hint.grid(row = 8, column = 3, sticky = "w")
D_label.grid(row = 9, column = 0)
D_entry.grid(row = 9, column = 1)
D_hint.grid(row = 9, column = 3, sticky = "w", columnspan = 2)
E_label.grid(row = 10, column = 0)
E_entry.grid(row = 10, column = 1)
E_hint.grid(row = 10, column = 3, sticky = "w")
F_label.grid(row = 11, column = 0)
F_entry.grid(row = 11, column = 1)
F_hint.grid(row = 11, column = 3, sticky = "w")
G_label.grid(row = 12, column = 0)
G_entry.grid(row = 12, column = 1)
G_hint.grid(row = 12, column = 3, sticky = "w")
H_label.grid(row = 13, column = 0)
H_entry.grid(row = 13, column = 1)
H_hint.grid(row = 13, column = 3, sticky = "w")
root.mainloop()
我猜这是pack
的问题,但我不知道如何解决它。默认情况下,pack
在顶部打包当前小部件,我猜我需要它在左上方打包inner_frame。由于它不允许2个参数用于方向或中心作为方向,是什么问题?
先谢了
3条答案
按热度按时间h43kikqp1#
首先,我不会使用
pack
,我会使用grid
。其次,请尝试查看所示的语法here.。因此,制作如下滚动条:w=scrollbar( master, option/options, … )
这个有用吗?
i86rm4rw2#
我解决了问题,
屏幕截图结果:
k3fezbri3#
多亏了acw1668,我设法使用
grid
解决了这个问题:具有非空权重的
grid_rowconfigure
和grid_columnconfigure
方法确保为滚动条分配额外的空间。