【发布时间】:2021-11-25 08:58:45
【问题描述】:
所以我在制作带有网格的水平滚动条时遇到了麻烦,并且一直在尝试混合和匹配不同的参数等,我在本教程中遇到了困难 https://newbedev.com/tkinter-canvas-scrollbar-with-grid 作为第一个例子 这是我目前的代码
window = tk.Tk()
window.geometry("1200x600")
frame_main = tk.Frame(window, bg="gray")
frame_main.grid(sticky='news')
# Create a frame for the canvas with non-zero row&column weights
frame_canvas = tk.Frame(frame_main)
frame_canvas.grid(row=0, column=0, pady=(5, 0), sticky='nw')
frame_canvas.grid_rowconfigure(0, weight=1)
frame_canvas.grid_columnconfigure(0, weight=1)
# Add a canvas in that frame
canvas = tk.Canvas(frame_canvas, bg="yellow")
canvas.grid(row=2, column=2, sticky="news")
# Link a scrollbar to the canvas
vsb = tk.Scrollbar(window, orient="horizontal", command=canvas.xview)
vsb.grid(row=0, column=2, sticky='we')
canvas.configure(xscrollcommand=vsb.set)
frame_canvas.config(width=first5columns_width + vsb.winfo_width(),height=first5rows_height)
canvas.config(scrollregion=canvas.bbox("all"))
col_0_head = tk.Label(window, text = " Adventures_Sherlock_Holmes.txt ", pady = 20) # pady = 20 gives some vertical
# separation between this row and the next
col_0_head.grid(row = 0, column = 0)
col_1_head = tk.Label(window, text = " Age_Innocence.txt ")
col_1_head.grid(row = 0, column = 1)
col_2_head = tk.Label(window, text = " Alice_Wonderland.txt ")
col_2_head.grid(row = 0, column = 2)
window.mainloop()
【问题讨论】:
-
重新考虑应用程序的布局并将 正确 父级分配给这些小部件。如果使用
grid(),也将它们放入正确 单元格中。有时使用pack()比使用grid()更容易。
标签: python python-3.x user-interface tkinter