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

时间:2014-07-14 20:55:29

标签: python python-2.7 tkinter

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

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

我知道这可以通过创建一个带有所需文本的Label并将两者精确定位在彼此附近来完成,但我更愿意避免使用该方法。

一些示例代码:

from Tkinter import * 
root = Tk()
Checkbutton(root, text="checkButon Text").grid()
root.mainloop()

1 个答案:

答案 0 :(得分:1)

嗯,我认为你不能直接直接,但你可以做一些看起来应该做的事情。这是标签解决方案,但我稍微改了一下,因此CheckbuttonLabel的结果化合物被视为包含在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()

当您创建多个框架(包含各自的内容 - CheckbuttonLabel)时,您可以轻松处理它们。这样你只需要像使用Checkbuttons一样定位Frames。