from Tkinter import *
import Pmw
import sys
class MenuBarDemo( Frame ):
def __init__( self ):
Frame.__init__( self )
Pmw.initialise()
self.pack( expand = YES, fill = BOTH )
self.master.title( "MenuBar Demo" )
self.master.geometry( "500x200" )
self.myBalloon = Pmw.Balloon( self )
self.choices = Pmw.MenuBar( self, balloon = self.myBalloon )
self.choices.pack( fill = X )
self.choices.addmenu( "File", "Exit" )
self.choices.addmenuitem( "File", "command", command = self.closeDemo, label = "Exit" )
self.choices.addmenu( "Format", "Change font/color" )
self.choices.addcascademenu( "Format", "Color" )
self.choices.addmenuitem( "Format", "separator" )
self.choices.addcascademenu( "Format", "Font" )
colors = [ "Black", "Blue", "Red", "Green" ]
self.selectedColor = StringVar()
self.selectedColor.set( colors[ 0 ] )
for item in colors:
self.choices.addmenuitem( "Color", "radiobutton",
label = item, command = self.changeColor,
variable = self.selectedColor )
fonts = [ "Times", "Courier", "Helvetica" ]
self.selectedFont = StringVar()
self.selectedFont.set( fonts [ 0 ] )
for item in fonts:
self.choices.addmenuitem( "Font", "radiobutton",
label = item, command = self.changeFont,
variable = self.selectedFont )
self.choices.addmenuitem( "Font", "separator" )
self.boldOn = BooleanVar()
self.choices.addmenuitem( "Font", "checkbutton",label = "Bold", command = self.changeFont,variable = self.boldOn )
self.italicOn = BooleanVar()
self.choices.addmenuitem( "Font", "checkbutton",
label = "Italic", command = self.changeFont,
variable = self.italicOn )
self.display = Canvas( self, bg = "white" )
self.display.pack( expand = YES, fill = BOTH )
self.sampleText = self.display.create_text( 250, 100,text = "Sample Text", font = "Times 48" )
def changeColor( self ):
self.display.itemconfig( self.sampleText,fill = self.selectedColor.get() )
def changeFont( self ):
newFont = self.selectedFont.get() + " 48"
if self.boldOn.get():
newFont += " bold"
if self.italicOn.get():
newFont += " italic"
self.display.itemconfig( self.sampleText, font = newFont )
def closeDemo( self ):
sys.exit()
MenuBarDemo().mainloop()
|