Python 3 tkinter: Switch Button

10,582

The tkinter Radiobutton is designed to do this. The key is to use the indicatoron option. When that option is set to false, instead of a little diamond or circle, you get a normal button that is sunken when the radiobutton is selected, and raised when it is not.

Example

import tkinter as tk

root = tk.Tk()

switch_frame = tk.Frame(root)
switch_frame.pack()

switch_variable = tk.StringVar(value="off")
off_button = tk.Radiobutton(switch_frame, text="Off", variable=switch_variable,
                            indicatoron=False, value="off", width=8)
low_button = tk.Radiobutton(switch_frame, text="Low", variable=switch_variable,
                            indicatoron=False, value="low", width=8)
med_button = tk.Radiobutton(switch_frame, text="Medium", variable=switch_variable,
                            indicatoron=False, value="medium", width=8)
high_button = tk.Radiobutton(switch_frame, text="High", variable=switch_variable,
                             indicatoron=False, value="high", width=8)
off_button.pack(side="left")
low_button.pack(side="left")
med_button.pack(side="left")
high_button.pack(side="left")

root.mainloop()

The above code will give you something that looks like the following screenshot. I clicked on the "Medium" button before taking the screenshot:

screenshot

Share:
10,582

Related videos on Youtube

eeqesri
Author by

eeqesri

Updated on June 04, 2022

Comments

  • eeqesri
    eeqesri almost 2 years

    I want to create my own widget with the following description: There are three buttons labelled low, medium, high. At any time either none of them is activated or exactly one. The one which is activated should have a sunken relief. If I click on one button the relief of all other buttons should be raised while the activated button should be sunken. How do I implement this on tkinter python3? I was able to do it for one button: This function is defined prior:

    def toggleLowDeer():
        if DeerLowButton.config('relief')[-1] == 'sunken':
            DeerLowButton.config(relief="raised")
        else:
            DeerLowButton.config(relief="sunken")
    

    The button is created afterwards

    DeerLowButton = Button(root, text="L", width=1, relief="raised", 
    command=toggleLowDeer)
    DeerLowButton.place(x=10, y=280)
    
    • Mike - SMT
      Mike - SMT about 6 years
      You can use a combination of buttons and conditions that can be applied per click with bindings. However you will need to attempt this on your own first and when/if you get stuck you can ask your question here with the code you have attempted.
    • Russell Smith
      Russell Smith about 6 years
      I strongly feel this should be reopened. While the original question didn't have an example, I don't think it needed one. It's a good question about an edge case of how to use a widget. Since the user doesn't know how to use the feature (or even know that the feature even exists), there's no point in showing example code.
    • Russell Smith
      Russell Smith about 6 years
      @touchmybody: I think in this case no code was needed. The OP simply doesn't know what feature to used, and is asking if such a feature exists. I hope you will reconsider your downvote and close vote (assuming you voted that way).
    • Russell Smith
      Russell Smith about 6 years
      @Mike-SMT: you don't need anything that complicated. Tkinter can do exactly what the OP is asking for. I think in this case the OP simply didn't know this feature existed, so asking them to provide code for something they don't even know can be done is perhaps a bit unfair. I hope you rethink your vote on this one.
    • Mike - SMT
      Mike - SMT about 6 years
      @BryanOakley Asking them to show an attempt does not seam unfair to me. That said I agree that my suggestion is more complicated than needed as I was not aware of an existing method to perform this action. When I made my comment and my vote to close the OP had not posted any code at all. I will take your advise in this and rethink my vote shortly.
  • Adam893
    Adam893 over 4 years
    I think you mean to say "When that option is set to false" as setting it to true has the opposite effect of what you describe.