tkinter如何调整文本相对于复选框图像的位置?

2024-06-01 10:09:31 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以将复选框描述移近复选框

CheckBox

character_set = LabelFrame(text=" Example ")

checkbtn0 = PhotoImage(file="example_off.png")
checkbtn1 = PhotoImage(file="example_on.png")

checktestbox = IntVar()

checktest = Label(character_set, image=checkbtn0)
checktest.bind("<Button-1>", Custom)
checktest.grid(padx=8, pady=4, row=0, column=0)

checktext = Label(character_set, text="Upper case (A-Z)")
checktext.grid(row=0, column=1)

character_set.pack(fill="both", expand="yes", padx=4, pady=4)

Tags: textpngexamplelabelgrid复选框filerow
2条回答

Question: adjust text position besides checkbox

相关的


参考

  • The Tkinter Label Widget - ^{}

    compound=
    Controls how to combine text and image in the label. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text.

  • The Tkinter Place Geometry Manager

    place(**options)
    Used in specialized cases, most importantly, by compound widgets to position.

  • The Tkinter Checkbutton Widget

    Checkbutton(parent, text=<str>, variable=<tk.VariableClasses>)
    The checkbutton widget is used to choose between two distinct values (usually switching something on or off).


  1. 使用FrameLabel组合您自己的Checkbutton小部件

    class Checkbutton(tk.Frame):
        off_image = PhotoImage(...
        on_image = PhotoImage(...
    
        def __init__(self, parent, **kwargs):
            text = kwargs.pop('text', None)
            self.callback = kwargs.pop('callback', None)
            super().__init__(parent, **kwargs)
    
            self.chkbox = tk.Label(parent, image=Checkbutton.off_image)
            self.chkbox.grid(row=0, column=0)
            self.chkbox.bind("<Button-1>", self.on_click)
    
            self.label = tk.Label(parent, text=text)
            self.chkbox.grid(row=0, column=1)
    
        def on_click(self, event):
            # Implements exchanging image
            pass
    
    chkbtn = Checkbutton(root, text="Upper case (A-Z)", callback=Custom)
    chkbtn.grid(padx=8, pady=4, row=0, column=0)
    
  2. 使用Label(..., compound='left')

    checktext = Label(root, image=checkbtn0, text="Upper case (A-Z)", compound='left')
    
  3. 使用.place(x, y)

    checktest = Label(root, image=checkbtn0)
    checktest.place(x=..., y=...)
    
    checktext = Label(root, text="Upper case (A-Z)")
    checktext.place(x=..., y=...)
    
  4. class tk.Checkbutton继承,使用您自己的图像
    看到这个答案:checkbutton different image

    class Checkbutton(tk.Checkbutton):
        off_image = PhotoImage(...
        on_image = PhotoImage(...
    
        def __init__(self, parent, **kwargs):
            self.var = tk.IntVar()
            super().__init__(parent, 
                             image=Checkbutton.off_image, 
                             selectimage=Checkbutton.on_image, 
                             indicatoron=False,
                             onvalue=1, offvalue=0, variable=self.var,
                             **kwargs)
    

您正在使用

checktest.grid(padx=8, pady=4, row=0, column=0)

这将在8 pixels的左右两侧添加Label

要减少甚至设置为0,请改用此选项:

checktest.grid(padx=(8,0), pady=4, row=0, column=0)

这允许在左侧和右侧填充不同的值

padx(a,b)

  • a垫子在左边吗
  • b垫子对吗

我要说的是padx在HTML中类似于左填充和右填充。
如果完全删除padx,则左填充和右填充将是zero

相关问题 更多 >