[英]tkinter pack vs grid methods: Scrollbar appearances
Below shows the right bottom corner of a Tk window that consists of a frame that consists of a Canvas and horizontal and vertical scrollbars.下面显示了一个 Tk 窗口的右下角,该窗口由一个由 Canvas 和水平和垂直滚动条组成的框架组成。 This was achieved using the Grid method.
这是使用网格方法实现的。
I am not able to replicate the same look using the Pack method.我无法使用 Pack 方法复制相同的外观。 Below is what I get and the sample code giving such a look.
下面是我得到的和给出这样外观的示例代码。
How do I replicate the look by the grid method using the pack method?如何使用 pack 方法通过 grid 方法复制外观?
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
frame = ttk.Frame( root )
frame.pack()
ysb = ttk.Scrollbar( frame, orient='vertical' )
xsb = ttk.Scrollbar( frame, orient='horizontal' )
canvas = tk.Canvas( frame, width=1000, height=700, background='green')
canvas.create_rectangle( 100, 100, 900, 600, fill='yellow' )
canvas.configure( scrollregion=canvas.bbox(tk.ALL),
xscrollcommand=xsb.set,
yscrollcommand=ysb.set )
xsb.config( command=canvas.xview )
ysb.config( command=canvas.yview )
ysb.pack( side='right', fill='y', expand=1)
xsb.pack( side='bottom', fill='x', expand=1 )
canvas.pack( side='left', fill='both', expand=1 )
#ysb.grid( row=0, column=1, sticky='ns' )
#xsb.grid( row=1, column=0, sticky='ew' )
#canvas.grid( row=0, column=0, sticky='nsew' )
You can use pady
to achieve it:您可以使用
pady
来实现它:
h = xsb.winfo_reqheight() # get the height of horizontal scrollbar
ysb.pack(side='right', fill='y', pady=(0,h)) # set bottom pady
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.