在LabelFrame中定位Radiobuttons

时间:2018-05-16 14:11:46

标签: python tkinter radio-button alignment calculation

如何按对称方式对齐按钮,以便它们完美地坐在框架中并与两侧及其邻居保持相同的距离

我的方法是这里的,但它没有带来正确的结果......

class MyApproach():

    def __init__(self):
        self.radiobutton_name_list = {"radio1": "This_is_text",
                                  "radio2": "This_is_mid_text",
                                  "radio3": "This_is_loooooooong_text"}
        self.create_root()
        self.create_label_frame()
        self.create_radiobuttons()
        self.mainloop()

    def create_root(self):
        self.root = Tk()
        self.root.geometry("740x120")
        self.root.title("test")
        self.root.resizable(False, False)

    def create_label_frame(self):
        self.new_labelframe = LabelFrame(self.root, text='My Frame', relief=GROOVE, bd=2)
        self.new_labelframe.place(width=720, height=80, x=10, y=10)

    def create_radiobuttons(self):
        self.radiobutton_xposition = 10
        for radiobuttons in self.radiobutton_name_list:
            if not len(self.radiobutton_name_list) <= 4: break
            Radiobutton(self.new_labelframe, text=self.radiobutton_name_list[radiobuttons]).place(
                        x=self.radiobutton_xposition, y=10)
            self.radiobutton_xposition += len(self.radiobutton_name_list[radiobuttons])

    def mainloop(self):
        self.root.mainloop()


app = MyApproach()

我不知道如何进行计算,但是&#34; self.radiobutton_xposition + = len(self.radiobutton_name_list [radiobuttons])&#34;是不正确的(* =)我猜这是错误的方法

谢谢你们

2 个答案:

答案 0 :(得分:3)

一般来说,使用place作为几何管理器会给你带来比解决的问题更多的问题。你为什么不用pack?

from tkinter import *

class MyApproach():

    def __init__(self):
        self.radiobutton_name_list = {"radio1": "This_is_text",
                                  "radio2": "This_is_mid_text",
                                  "radio3": "This_is_loooooooong_text"}
        self.create_root()
        self.create_label_frame()
        self.create_radiobuttons()
        self.mainloop()

    def create_root(self):
        self.root = Tk()
        self.root.geometry("740x120")
        self.root.title("test")
        self.root.resizable(False, False)

    def create_label_frame(self):
        self.new_labelframe = LabelFrame(self.root, text='My Frame', relief=GROOVE, bd=2)
        self.new_labelframe.pack(expand=1, fill=BOTH, padx=10, pady=10)

    def create_radiobuttons(self):
        self.v = StringVar()
        self.v.set(' ')
        for radiobuttons in self.radiobutton_name_list:
            r = Radiobutton(self.new_labelframe, text=self.radiobutton_name_list[radiobuttons], variable=self.v, value=radiobuttons)
            r.pack(side=LEFT, padx=10, expand=1, fill=X)

    def mainloop(self):
        self.root.mainloop()


app = MyApproach()

答案 1 :(得分:1)

更新:

我能想到的最快的解决方案是为每列提供权重,然后将按钮放在那里。地方有其时刻,但大多数情况下,您可以使用pack()grid()解决展示位置问题。

这里我们将使用网格并生成一个范围的列号。

尝试以下方法:

from tkinter import *

class MyApproach():

    def __init__(self):
        # updated the data here so it would be easier to run a loop over it.
        self.radiobutton_name_list = {0: ["radio1", "This_is_text"],
                                  1: ["radio2", "This_is_mid_text"],
                                  2: ["radio3", "This_is_loooooooong_text"]}

        self.create_root()
        self.create_label_frame()
        self.create_radiobuttons()
        self.mainloop()

    def create_root(self):
        self.root = Tk()
        self.root.geometry("740x120")
        self.root.title("test")
        self.root.resizable(False, False)

    def create_label_frame(self):
        self.new_labelframe = LabelFrame(self.root, text='My Frame', relief=GROOVE, bd=2)
        self.new_labelframe.place(width=720, height=80, x=10, y=10)

    def create_radiobuttons(self):
        # using len and range to get the key of the radio button list.
        # this same key number can be used as the column as well.
        for i in range(len(self.radiobutton_name_list)):
            self.new_labelframe.columnconfigure(i, weight=1)
            Radiobutton(self.new_labelframe, text=self.radiobutton_name_list[i][1]).grid(row=0, column=i, sticky="nsew")

    def mainloop(self):
        self.root.mainloop()


app = MyApproach()