简体   繁体   English

如何在 tkinter 按钮之间添加特定的像素间距?

[英]How to add a specific spacing in pixels between tkinter buttons?

I have written some code for some buttons.我已经为一些按钮编写了一些代码。 However, I am not sure how to add a specific number of pixels of spacing for each button.但是,我不确定如何为每个按钮添加特定数量的间距像素。 So far is the code I have written.到目前为止是我写的代码。 However, I have not yet figured out a reliable way to add spacing between the buttons in pixel sizes.但是,我还没有找到一种可靠的方法来在像素大小的按钮之间添加间距。

import tkinter as tk
#from tkinter import PhotoImage

def banana():
    print ("Sundae")

def tomato():
    print ("Ketchup")

def potato():
    print ("Potato chips")

root = tk.Tk()
root.geometry("960x600")

f1 = tk.Frame(root, width=70, height=30)
f1.grid(row=3, column=0, sticky="we")

button_qwer = tk.Button(f1, text="Banana", command=banana)
button_asdf = tk.Button(f1, text="Tomato", command=tomato)
button_zxcv = tk.Button(f1, text="Potato", command=potato)

button_qwer.grid(row=0, column=0)
button_asdf.grid(row=0, column=1)
button_zxcv.grid(row=0, column=2)

root.mainloop()

Using a specific number of pixels of spacing between each Button doesn't sound to me like such as good idea because it isn't very flexible nor easily portable to devices with different resolutions.在每个Button之间使用特定数量的像素间距对我来说听起来不是一个好主意,因为它不是很灵活,也不容易移植到具有不同分辨率的设备上。

Nevertheless I've figured-out a way of doing it—namely by putting a do-nothing invisible button between of the each real ones.尽管如此,我还是想出了一种方法——即在每个真实按钮之间放置一个无所事事的隐形按钮。 This got somewhat involved, mostly because it requires putting an image on each Button used this way so its width option argument will be interpreted as number of pixels instead of number of characters (here's some documentation describing the various Button widget configuration options).这有点涉及,主要是因为它需要在以这种方式使用的每个Button上放置一个图像,因此它的width选项参数将被解释为像素数而不是字符数(这里是一些描述各种Button小部件配置选项的文档)。

import tkinter as tk

# Inline XBM format data for a 1x1 pixel image.
BITMAP = """
    #define im_width 1
    #define im_height 1
    static char im_bits[] = {
        0x00
    };
"""

root = tk.Tk()
root.geometry("960x600")
bitmap = tk.BitmapImage(data=BITMAP, maskdata=BITMAP)

f1 = tk.Frame(root, width=70, height=30)
f1.grid(row=3, column=0, sticky=tk.EW)

def banana():
    print ("Sundae")

def tomato():
    print ("Ketchup")

def potato():
    print ("Potato chips")

def layout_buttons(parent, buttons, spacing):
    if buttons:
        first, *rest = buttons
        first.grid(row=0, column=0)  # Position first Button.

        for index, button in enumerate(rest, start=1):
            col = 2*index
            # Dummy widget to separate each button from the one before it.
            separator = tk.Button(parent, relief=tk.FLAT, state=tk.ACTIVE,
                                  image=bitmap, borderwidth=0, highlightthickness=0,
                                  width=spacing)
            separator.grid(row=0, column=col-1)
            button.grid(row=0, column=col)

buttons = (
    tk.Button(f1, text="Banana", command=banana),
    tk.Button(f1, text="Tomato", command=tomato),
    tk.Button(f1, text="Potato", command=potato),
)

layout_buttons(f1, buttons, 30)
root.mainloop()

Result:结果:

带有按钮的窗口屏幕截图,按钮之间有空格

Here's a blow-up showing that the spacing is exactly 30 pixels (as counted in my image editor and indicated by the thin horizontal black line between the adjacent edges of the two Button s).这是一个放大图,显示间距正好是 30 像素(在我的图像编辑器中计算,并由两个Button的相邻边缘之间的细水平黑线指示)。

上一张截图的放大图

Adding space between widgets depends on how you are putting the widgets in the window.在小部件之间添加空间取决于您如何将小部件放入 window 中。 Since you are using grid , one simple solution is to leave empty columns between the buttons, and then give these columns a minsize equal to the space you want.由于您使用的是grid ,一个简单的解决方案是在按钮之间留下空列,然后给这些列的minsize等于您想要的空间。

Example:例子:

f1.grid_columnconfigure((1, 3), minsize=10, weight=0)

button_qwer.grid(row=0, column=0)
button_asdf.grid(row=0, column=2)
button_zxcv.grid(row=0, column=4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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