【问题标题】:Change position of checkbox relative to text in Tkinter's Checkbutton在 Tkinter 的 Checkbutton 中更改复选框相对于文本的位置
【发布时间】:2014-09-04 22:01:50
【问题描述】:

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

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

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

一些示例代码:

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

【问题讨论】:

  • 我认为你不能改变它。

标签: python python-2.7 tkinter


【解决方案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。

【讨论】:

  • 是的,这可能是最好的方法。谢谢。
  • 对我来说看起来有点矫枉过正。基本上你所做的只是创建一个Label 并将其放在左列,然后将Checkbutton 放在右列。这可以简单地开始,而不需要这样的类。
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2020-10-21
  • 2018-01-11
  • 1970-01-01
  • 2014-06-26
  • 2011-03-12
  • 2019-08-14
相关资源
最近更新 更多