#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2

    Border surrounding radiobottons - how is it done


    How is a thin retangular border created? In this code
    example, the 3 radiobuttons with text would have a border.
    Code:
    from tkinter import *
    
    root = Tk()
    
    root.geometry('300x200+20+20')
    
    var1 = IntVar()
    var2 = StringVar()
    
    def show_option():
        v1 = var1.get()
        selection = "You selected the option #" + str(var1.get())
        lblMessage.config(text=selection)
            
    r1 = Radiobutton(root, text="Option 1", variable=var1, value=1, command=show_option)
    r1.pack()
    r1.place(x=90, y=50)
    r1.select()
    
    r2 = Radiobutton(root, text="Option 2", variable=var1, value=2, command=show_option)
    r2.pack()
    r2.place(x=90, y=70)
    
    r3 = Radiobutton(root, text="Option 3", variable=var1, value=3, command=show_option)
    r3.pack()
    r3.place(x=90, y=90)
    
    var2 = StringVar()
    lblMessage = Label(root)
    lblMessage.pack()
    lblMessage.place(x = 60,  y = 150)
    
    mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Put the buttons into a container, often a Frame. Set the borderwidth positive and choose relief.

    Code:
    # this is a python 3 code.  Use Tkinter for python 2.
    from tkinter import *
    
    root = Tk()
    
    root.geometry('300x200+20+20')
    
    var1 = IntVar()
    
    def show_option():
        v1 = var1.get()
        selection = "You selected the option #" + str(var1.get())
        lblMessage.config(text=selection)
            
    frame = Frame(root, borderwidth = 12, relief = SUNKEN)
    frame.pack()
    
        
    
    r1 = Radiobutton(frame, text="Option 1", variable=var1, value=1, command=show_option)
    r1.pack()
    r1.select()
    
    r2 = Radiobutton(frame, text="Option 2", variable=var1, value=2, command=show_option)
    r2.pack()
    
    
    r3 = Radiobutton(frame, text="Option 3", variable=var1, value=3, command=show_option)
    r3.pack()
    
    
    lblMessage = Label(root)
    lblMessage.pack()
    
    mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2
    Once again, I am enlightened by this forum.
    A frame with borderwidth=2 and relief=GROOVE gives the
    illusion of a thin line for a border.
    Many thanks!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Hi, everybody, I am a new friend.


    Hi, everybody, I am a new friend.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    For radio buttons a LabelFrame is ideal ...
    Code:
    ''' tk_RadioButton_multi3.py
    explore Tkinter's Radiobutton widget
    create multiple radio buttons with list comprehension
    '''
    
    try:
        # Python2
        import Tkinter as tk
    except ImportError:
        # Python3
        import tkinter as tk
    
    def click():
        """shows the value of the radio button selected"""
        root.title(vs.get())
    
    root = tk.Tk()
    
    # create a labeled frame for the radiobuttons
    # relief='groove' and labelanchor='nw' are default
    labfr = tk.LabelFrame(root, text=" select one ", bd=3)
    labfr.pack(padx=55, pady=10)
    
    # use a string variable
    vs = tk.StringVar()
    
    values = ['eggs', 'spam', 'cheese', 'bacon']
    # create a list of Radiobutton instances
    radiobuttons = [tk.Radiobutton(labfr, text=val, value=val,
        variable=vs, command=click) for val in values]
    
    # use the pack() layout manager to position the radio buttons
    [rb.pack(anchor='w') for rb in radiobuttons]
    
    # needed, set to None or any of the radiobutton values
    vs.set('spam')
    # show value set
    if vs:
        click()
    
    root.mainloop()

    Comments on this post

    • b49P23TIvg agrees : Great! Where do you find this stuff? OK, it is in the book.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo