更改 Tkinter 的 Checkbutton 中复选框相对于文本的位置

Mos*_*she 6 python tkinter python-2.7

如何更改文本相对于 Tkinter 复选框的位置Checkbutton

默认情况下,文本位于复选框的右侧。我想更改它,因此文本位于复选框的左侧或上方。

我知道这可以通过创建一个Label包含所需文本的文本并将两者巧妙地放置在彼此附近来完成,但我宁愿避免这种方法。

一些示例代码:

from Tkinter import * 
root = Tk()
Checkbutton(root, text="checkButon Text").grid()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

小智 4

好吧,我不认为你可以直接做到这一点,但你可以做一些看起来应该做的事情。Checkbutton它是 Label 解决方案,但我稍微改变了它,因此和的结果复合Label被视为包装在Frame.

from Tkinter import *

class LabeledCheckbutton(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        self.checkbutton = Checkbutton(self)
        self.label = Label(self)
        self.label.grid(row=0, column=0)
        self.checkbutton.grid(row=0, column=1)


root = Tk()
labeledcb = LabeledCheckbutton(root)
labeledcb.label.configure(text="checkButton Text")
labeledcb.grid(row=0, column=0)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

当您创建多个框架(及其各自的内容 -CheckbuttonLabel)时,您可以轻松处理它们。这样,您只需像使用检查按钮一样定位框架即可。