How can I move the text label of a radiobutton below the button in Python Tkinter?

后端 未结 3 502
旧巷少年郎
旧巷少年郎 2020-12-20 20:25

I\'m wondering if there\'s a way to move the label text of a radiobutton to a different area, e.g. below the actual button.

Below is an example of a few radiobuttons

3条回答
  •  臣服心动
    2020-12-20 21:15

    As Sun Bear suggested, another method to get the radiobutton label beneath the button is to customize the ttk style:

    from tkinter import *
    from tkinter import ttk
    
    class MainGUI(Tk):
    
        def __init__(self):
            Tk.__init__(self)
            self.geometry("300x100")
            self.configure(background="white")
            self.mainframe = ttk.Frame(self, padding="8 8 12 12")
            self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
            self.mainframe.columnconfigure(0, weight=1)
            self.mainframe.rowconfigure(0, weight=1)
            style = ttk.Style(self)
            style.theme_use('clam')
            # create custom radiobutton style layout
            style.layout('CustomRadiobutton', 
                         [('Radiobutton.padding',
                           {'children': 
                               [('Radiobutton.indicator', 
                                 {'side': 'top', 'sticky': ''}), # changed side from left to top
                                ('Radiobutton.focus',
                                 {'children': [('Radiobutton.label', {'sticky': 'nswe'})],
                                  'side': 'top', # changed side from left to top
                                  'sticky': ''})],
                            'sticky': 'nswe'})])
            style.configure('TFrame', background="white")
            style.configure('CustomRadiobutton', background="white")
            ttk.Radiobutton(self.mainframe, style='CustomRadiobutton',
            text="Button 1", value=0).grid(column=1, row=2, padx=4)
            ttk.Radiobutton(self.mainframe, style='CustomRadiobutton',
            text="Button 2", value=1).grid(column=2, row=2, padx=4)
            ttk.Radiobutton(self.mainframe, style='CustomRadiobutton',
            text="Button 3", value=2).grid(column=3, row=2, padx=4)
            ttk.Radiobutton(self.mainframe, style='CustomRadiobutton',
            text="Button 4", value=3).grid(column=4, row=2, padx=4)
            self.mainloop()
    
    if __name__ == '__main__':
        app = MainGUI()
    

    However, this does not work with all ttk themes. It works with clam and alt, but gives buttons that cannot be selected with classic and default in linux.

提交回复
热议问题