简体   繁体   中英

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. 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.

For some reason, the scrollbar is the only widget that naturally places a pady attribute.

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) . 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM