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

后端 未结 3 504
旧巷少年郎
旧巷少年郎 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:14

    There is no option to set the text position in a radiobutton, so the easiest workaround I can think of is to remove the radiobutton text and put a label beneath it.

    In addition, I would rather make a MainGUI class inherit from Tk instead of Frame so that you don't have to create a root window in the if __name__ == '__main__': section, but it's just a suggestion.

    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)
            ttk.Radiobutton(self.mainframe,  value=0).grid(column=1, row=2)
            ttk.Label(self.mainframe, text="Button 1").grid(column=1, row=3, padx=4)
            ttk.Radiobutton(self.mainframe,  value=1).grid(column=2, row=2)
            ttk.Label(self.mainframe, text="Button 2").grid(column=2, row=3, padx=4)
            ttk.Radiobutton(self.mainframe,  value=2).grid(column=3, row=2)
            ttk.Label(self.mainframe, text="Button 3").grid(column=3, row=3, padx=4)
            ttk.Radiobutton(self.mainframe,  value=3).grid(column=4, row=2)
            ttk.Label(self.mainframe, text="Button 4").grid(column=4, row=3, padx=4)
            self.mainloop()
    
    if __name__ == '__main__':
        app = MainGUI()
    

    Remark: to get this picture, I also used a ttk.Style to get a nicer result in linux.

提交回复
热议问题