[英]Python - tkinter remove space between scrollbar and entry widgets
In my program I have a ttk.Scrollbar
which works in tandem with a read-only ttk.Entry
widget. 在我的程序中,我有一个
ttk.Scrollbar
,它与只读ttk.Entry
小部件ttk.Entry
。 The problem I have is that there is a spacing between the bottom of the entry widget and the top of the scrollbar widget. 我的问题是条目小部件的底部和滚动条小部件的顶部之间有一个间距。 I want to get rid of that but
tkinter
doesn't allow you to use negative padding and such. 我想摆脱它,但
tkinter
不允许您使用负填充等。
For some reason, the scrollbar is the only widget that naturally places a pady
attribute. 出于某种原因,滚动条是唯一自然放置
pady
属性的窗口小部件。
Below is the code, the scrollbar widget and the relative entry widget commented out. 下面是代码,注释了滚动条小部件和相对条目小部件。
from tkinter import *
from tkinter import ttk
def calculate(*args):
try:
value = int(binary.get(), 2)
decimal.set(value)
except ValueError:
error = "Please enter a binary value."
decimal.set(error)
pass
root = Tk() #set object `root` to `Tk()`
root.title("Binary to Decimal Converter")
root.wm_iconbitmap("binaryicon.ico")
root.grid_columnconfigure(0, weight = 1)
root.resizable(False, False)
mainframe = ttk.Frame(root, padding = "3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
binary = StringVar()
decimal = StringVar()
binary_entry = ttk.Entry(mainframe, width = 32, textvariable = binary)
binary_entry.grid(column = 2, row = 1, sticky = (W, E))
"""decimalView = ttk.Entry(mainframe, state = "readonly", background = "gray99", width = 32, textvariable = decimal)
decimalView.grid(column = 2, row = 2, sticky = W)
scrollbar = ttk.Scrollbar(mainframe, orient = HORIZONTAL, command = decimalView.xview)
scrollbar.grid(column = 2, row = 3, sticky = (N, S, E, W))
decimalView.configure(xscrollcommand = scrollbar.set)"""
ttk.Button(mainframe, text = "Calculate", command = calculate).grid(column = 3, row = 3, sticky = W)
ttk.Label(mainframe, text = "Binary").grid(column = 3, row = 1, sticky = W)
ttk.Label(mainframe, text = "Decimal").grid(column = 3, row = 2, sticky = W)
binary_entry.focus()
root.bind("<Return>", calculate)
root.mainloop()
In this specific case, the simplest solution is to use sticky=(N, E, W)
. 在这种特定情况下,最简单的解决方案是使用
sticky=(N, E, W)
。 Your code currently makes it sticky to both the top and bottom of the cell (both N
and S
), causing it to ether stretch or be centered in the cell (on the Mac, for instance, scrollbars are a fixed size, so they will be centered rather than stretched). 您的代码当前使它同时粘在单元格的顶部和底部(
N
和S
),从而导致其以太拉伸或居中于单元格(例如,在Mac上,滚动条的大小是固定的,因此它们会居中而不是伸展)。 Since you want it to "stick" to the bottom of the widget immediately above it, you don't want it to also "stick" to the bottom of its own cell. 由于您希望它“粘贴”在其上方的小部件底部,因此您不希望它也“粘贴”在其自己单元格的底部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.