Use Radio buttons to change the font : Radiobutton « Tkinker « Python Tutorial






Use Radio buttons to change the font
from Tkinter import *

class RadioFont( Frame ):
   def __init__( self ):
      Frame.__init__( self )
      self.pack( expand = YES, fill = BOTH )
      self.master.title( "Radiobutton Demo" ) 

      self.frame1 = Frame( self )
      self.frame1.pack()
      
      self.text = Entry( self.frame1, width = 40,font = "Arial 10" )
      self.text.insert( INSERT, "Watch the font style change" )
      self.text.pack( padx = 5, pady = 5 )

      self.frame2 = Frame( self )
      self.frame2.pack()
      
      fontSelections = [ "Plain", "Bold", "Italic","Bold/Italic" ]
      self.chosenFont = StringVar()

      self.chosenFont.set( fontSelections[ 0 ] ) 

      for style in fontSelections:
         aButton = Radiobutton( self.frame2, text = style,variable = self.chosenFont, value = style,command = self.changeFont )
         aButton.pack( side = LEFT, padx = 5, pady = 5 )

   def changeFont( self ):
      desiredFont = "Arial 10"

      if self.chosenFont.get() == "Bold":
         desiredFont += " bold"
      elif self.chosenFont.get() == "Italic":
         desiredFont += " italic"
      elif self.chosenFont.get() == "Bold/Italic":
         desiredFont += " bold italic"

      self.text.config( font = desiredFont )

RadioFont().mainloop()








18.26.Radiobutton
18.26.1.Radio buttons, the easy wayRadio buttons, the easy way
18.26.2.Hold on to your radio variables (an obscure thing, indeed)Hold on to your radio variables (an obscure thing, indeed)
18.26.3.Deselect for radio buttons simply sets the button's associated value to a null stringDeselect for radio buttons simply sets the button's associated value to a null string
18.26.4.See what happens when some buttons have same valueSee what happens when some buttons have same value
18.26.5.Add RadioButton to a groupAdd RadioButton to a group
18.26.6.Bind variable to RadioButtonBind variable to RadioButton
18.26.7.Radio button barRadio button bar
18.26.8.Use Radio buttons to change the fontUse Radio buttons to change the font