Search code examples
tkintermenuradio-buttontkinter-button

Tkinter How to make one button of radiobuttons do as a menubutton?


enter image description hereI have a group of buttons that work together as radiobuttons, and I try to make the last one of them open a menu like menubutton Or at least make the menubutton separate and make it work by clicking a button in the radiobuttons group

menubutton does not accept value and variable


Solution

  • I made a simple example with the concept you describe

    import tkinter as tk
    
    root = tk.Tk()
    
    popup = tk.Menu(root, tearoff=0)
    popup.add_command(label="Popup1")
    popup.add_command(label="Popup2")
    
    
    def popup_command(btn):
        try:
            x = btn.winfo_rootx()
            y = btn.winfo_rooty()
            popup.tk_popup(x, y, 0)
        finally:
            popup.grab_release()
    
    
    v = tk.StringVar(root, "1")
    values = {"Standard Button 1": "1",
              "Standard Button 2": "2",
              "Standard Button 3": "3",
              "Standard Button 4": "4",
              "Pop-out menu button": "5"}
    
    for text, value in values.items():
        btn = tk.Radiobutton(root, text=text, variable=v, value=value)
        if value == "5":
            btn.configure(command=lambda: popup_command(btn))
        btn.pack()
    
    root.mainloop()