简体   繁体   中英

condensing/reducing vertical spacing between widgets in tkinter (Python) (AKA: setting vertical line/text spacing/padding)

Here's my program:

import tkinter as tk 

#Create main window object 
root = tk.Tk()

#build GUI
for i in range(5):
    tk.Label(root, text="hello", height=0).grid(row=i)

#mainloop
root.mainloop()

It produces the following (running in Xubuntu 16.04 LTS)

在此处输入图片说明

Notice all that extra vertical space between the lines of text. I don't want that! How do I decrease it?

If I run this code instead:

import tkinter as tk 

#Create main window object 
root = tk.Tk()

#build GUI
for i in range(5):
    tk.Label(root, text="hello", height=0).grid(row=i)
    tk.Grid.rowconfigure(root, i, weight=1) #allow vertical compression/expansion to fit window size after manual resizing

#mainloop
root.mainloop()

...it opens up and initially looks exactly the same as before, but now I can manually drag the box vertically to shrink it, like so:

在此处输入图片说明

Notice how much more vertically-compressed it is! But, how do I do this programatically , so I don't have to manually drag it to make it this way? I want to set this tight vertical spacing from the start, but no matter which parameters I change in Label , grid , or rowconfigure I can't seem to make it work without me manually dragging the box with the mouse to resize and vertically compress the text.

You can programatically modify the geometry just before starting the main loop instead of manually dragging it (change 0.6 to whatever % reduction you want):

import tkinter as tk  

#Create main window object 
root = tk.Tk()

#build GUI
for i in range(5):
    label = tk.Label(root, text = 'hello')
    label.grid(row=i)
    tk.Grid.rowconfigure(root, i, weight=1) #allow vertical compression/expansion to fit window size after manual resizing

#mainloop
root.update()
root.geometry("{}x{}".format(root.winfo_width(), int(0.6*root.winfo_height())))
root.mainloop()

Here is a screenshot of the result running on Xubuntu 16.04 LTS with Python 3.5.2:

在此处输入图片说明

There are many ways to affect vertical spacing.

When you use grid or pack there are options for padding (eg: pady , ipady , minsize ). Also, the widget itself has many options which control its appearance. For example, in the case of a label you can set the borderwidth , highlightthickness and pady values to zero in order to make the widget less tall.

Different systems have different default values for these various options, and for some of the options the default is something bigger than zero. When trying to configure the visual aspects of your GUI, the first step is to read the documentation, and look for options that affect the visual appearance. Then, you can start experimenting with them to see which ones give you the look that you desire.

In your specific case, this is about the most compact you can get:

label = tk.Label(root, highlightthickness=0, borderwidth=0, pady=0, text="hello")
label.grid(row=i, pady=0, ipady=0)

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