tkinter:如何对齐文本长度不同的一组单选按钮

时间:2017-05-04 17:15:08

标签: python tkinter

tech_radioButton1 = ttk.Radiobutton(selection_frame_1, text="ANT+", variable = self.tech_var, value = 1, command = self.tech_select)
tech_radioButton1.pack(side = 'top')
tech_radioButton2 = ttk.Radiobutton(selection_frame_1, text="Bluetooth", variable = self.tech_var, value = 2, command = self.tech_select)
tech_radioButton2.pack(side = 'top')
tech_radioButton3 = ttk.Radiobutton(selection_frame_1, text="BTLE", variable = self.tech_var, value = 3, command = self.tech_select)
tech_radioButton3.pack(side = 'top')
tech_radioButton4 = ttk.Radiobutton(selection_frame_1, text="WLAN", variable = self.tech_var, value = 4, command = self.tech_select)
tech_radioButton4.pack(side = 'top')
tech_radioButton5 = ttk.Radiobutton(selection_frame_1, text="UNII", variable = self.tech_var, value = 5, command = self.tech_select)
tech_radioButton5.pack(side = 'top')

我的项目中有一组单选按钮的代码段,当我运行它时看起来像这样:

enter image description here

我想将它们全部固定在西边,这样它们的左侧就会垂直对齐。我尝试使用tk代替ttk(因为在ttk中没有锚选项)radiobutton并设置anchor ='w',但它们仍然如下所示: enter image description here

有人可以帮忙吗? TIA

1 个答案:

答案 0 :(得分:1)

没关系,我发现了。包装时我应该做锚='w'。如下所示:

tech_radioButton1 = ttk.Radiobutton(selection_frame_1, text="ANT+", variable = self.tech_var, value = 1, command = self.tech_select)
tech_radioButton1.pack(side = 'top', anchor = 'w')
tech_radioButton2 = ttk.Radiobutton(selection_frame_1, text="Bluetooth", variable = self.tech_var, value = 2, command = self.tech_select)
tech_radioButton2.pack(side = 'top', anchor = 'w')
tech_radioButton3 = ttk.Radiobutton(selection_frame_1, text="BTLE", variable = self.tech_var, value = 3, command = self.tech_select)
tech_radioButton3.pack(side = 'top', anchor = 'w')
tech_radioButton4 = ttk.Radiobutton(selection_frame_1, text="WLAN", variable = self.tech_var, value = 4, command = self.tech_select)
tech_radioButton4.pack(side = 'top', anchor = 'w')
tech_radioButton5 = ttk.Radiobutton(selection_frame_1, text="UNII", variable = self.tech_var, value = 5, command = self.tech_select)
tech_radioButton5.pack(side = 'top', anchor = 'w')