def __init__(self): DemoWindow.__init__(self, '', 'tagged-text.py' ) frame = Frame(self); frame.pack(expand=Y, fill=BOTH ) text=Text(frame, relief=SUNKEN, bd=2, setgrid=1, wrap='word', height=35); text.pack(side=LEFT, expand=Y, fill=BOTH) bar=Scrollbar(frame); bar.pack(side=LEFT, fill=Y) text['yscrollcommand']=bar.set; bar['command']=text.yview self.text = text self.tags = range(0,6) # create and binds the tags for t in self.tags: self.text.tag_configure (t) self.text.tag_bind(t, '<Any-Enter>', callit(self.reconfigure_tag, t, 'bold' )) self.text.tag_bind(t, '<Any-Leave>', callit(self.reconfigure_tag, t, 'normal' )) self.text.tag_bind(t, '<1>', callit(self.link_callback, t )) # insert tagged text intro = """ The same tag mechanism that controls display styles in text widgets can also be used to associate Tcl commands with regions of text, so that mouse or keyboard actions on the text cause particular Tcl commands to be invoked. For example, in the text below the descriptions of the canvas demonstrations have been tagged. When you move the mouse over a demo description the description lights up, and when you press button 1 over a description then that particular demonstration is invoked. """ text.insert('0.0', intro ) text.insert( END, '1. Samples of all the different types of items that'+ ' can be created in canvas widgets.', self.tags[0] ) text.insert( END, '\n\n'); text.insert( END, '2. A simple two-dimensional plot that allows you '+ 'to adjust the positions of the data points.', self.tags[1] ) text.insert( END, '\n\n'); text.insert( END, '3. Anchoring and justification modes for text items.', self.tags[2] ) text.insert( END, '\n\n'); text.insert( END, '4. An editor for arrow-head shapes for line items.', self.tags[3] ) text.insert( END, '\n\n'); text.insert( END, '5. A ruler with facilities for editing tab stops.', self.tags[4] ) text.insert( END, '\n\n'); text.insert( END, '6. A grid that demonstrates how canvases can be '+ 'scrolled.', self.tags[5] )
def __init__(self): DemoWindow.__init__(self, '', 'bind.py' ) frame = Frame(self); frame.pack(expand=Y, fill=BOTH ) text=Text(frame, relief=SUNKEN, bd=2, setgrid=1, wrap='word', height=35); text.pack(side=LEFT, expand=Y, fill=BOTH) bar=Scrollbar(frame); bar.pack(side=LEFT, fill=Y) text['yscrollcommand']=bar.set; bar['command']=text.yview self.text = text self.tags = range(0,6) # create and binds the tags for t in self.tags: self.text.tag_configure (t) self.text.tag_bind(t, '<Any-Enter>', callit(self.reconfigure_tag, t, 'bold' )) self.text.tag_bind(t, '<Any-Leave>', callit(self.reconfigure_tag, t, 'normal' )) self.text.tag_bind(t, '<1>', callit(self.link_callback, t )) # insert tagged text intro = """ The same tag mechanism that controls display styles in text widgets can also be used to associate Tcl commands with regions of text, so that mouse or keyboard actions on the text cause particular Tcl commands to be invoked. For example, in the text below the descriptions of the canvas demonstrations have been tagged. When you move the mouse over a demo description the description lights up, and when you press button 1 over a description then that particular demonstration is invoked. """ text.insert('0.0', intro ) text.insert( END, '1. Samples of all the different types of items that'+ ' can be created in canvas widgets.', self.tags[0] ) text.insert( END, '\n\n'); text.insert( END, '2. A simple two-dimensional plot that allows you '+ 'to adjust the positions of the data points.', self.tags[1] ) text.insert( END, '\n\n'); text.insert( END, '3. Anchoring and justification modes for text items.', self.tags[2] ) text.insert( END, '\n\n'); text.insert( END, '4. An editor for arrow-head shapes for line items.', self.tags[3] ) text.insert( END, '\n\n'); text.insert( END, '5. A ruler with facilities for editing tab stops.', self.tags[4] ) text.insert( END, '\n\n'); text.insert( END, '6. A grid that demonstrates how canvases can be '+ 'scrolled.', self.tags[5] )
def __init__(self): intro = """If you click on any of the four buttons below, the background of the button area will change to the color indicated in the button. You can press Tab to move among the buttons, then press Space to invoke the current button.""" infrastructure.DemoWindow.__init__(self, intro, 'button.py') self.frame = Frame(self) self.frame.pack(expand=YES, fill=BOTH) for c in ('Peach Puff', 'Light Blue', 'Sea Green', 'Yellow'): b = Button(self.frame, text=c) b['command'] = infrastructure.callit(self.callback, c) b.pack(side=TOP, expand=YES, pady=2)
def __init__( self ): intro="""If you click on any of the four buttons below, the background of the button area will change to the color indicated in the button. You can press Tab to move among the buttons, then press Space to invoke the current button.""" infrastructure.DemoWindow.__init__(self, intro, 'button.py' ) self.frame=Frame(self) self.frame.pack(expand=YES, fill=BOTH ) for c in ('Peach Puff', 'Light Blue', 'Sea Green', 'Yellow' ): b = Button(self.frame, text=c) b['command'] = infrastructure.callit( self.callback, c ); b.pack( side=TOP, expand=YES, pady=2 )
def __init__(self): l = """A 15-puzzle appears below as a collection of buttons. Click on any of the pieces next to the space, and that piece will slide over the space. Continue this until the pieces are arranged in numerical order from upper-left to lower-right.""" infrastructure.DemoWindow.__init__(self, l, 'puzzle.py') # create the buttons container frame = Frame(self, relief=SUNKEN, border=2, background='darkgray', width=100, height=100) frame.pack(pady=10, expand=NO, fill=NONE) # define the initial position start_sequence = [x for x in range(1, 16)] + [ 0, ] # create and place buttons self.buttons = [] # this will be a matrix cnt = 0 for v in start_sequence: r, c = cnt // 4, cnt % 4 if cnt % 4 == 0: self.buttons.append([]) # create a new row if v: # zero means empty place b = PuzzleButton(frame, v, r, c) b['command'] = infrastructure.callit(self.callback, b) self.buttons[r].append(b) else: self.buttons[r].append(None) cnt = cnt + 1 # make a number of arbitrary moves to scramble the puzzle for i in range(1, 1000): row = random.choice(self.buttons) b = random.choice(row) if b: self.callback(b)
def __init__(self): l ="""A 15-puzzle appears below as a collection of buttons. Click on any of the pieces next to the space, and that piece will slide over the space. Continue this until the pieces are arranged in numerical order from upper-left to lower-right.""" infrastructure.DemoWindow.__init__(self, l, 'puzzle15.py' ) # create the buttons container frame = Frame(self, relief=SUNKEN, border=2, background='darkgray', width=100, height=100) frame.pack( pady=10,expand=NO, fill=NONE) # define the initial position start_sequence = [x for x in range(1,16)]+[0,] # create and place buttons self.buttons = [] # this will be a matrix cnt=0 for v in start_sequence: r, c = cnt//4, cnt%4 if cnt % 4 == 0 : self.buttons.append([]) # create a new row if v : # zero means empty place b = PuzzleButton(frame, v, r, c ) b['command'] = infrastructure.callit(self.callback, b) self.buttons[r].append(b) else: self.buttons[r].append(None) cnt=cnt+1 # make a number of arbitrary moves to scramble the puzzle for i in range(1,1000): row = random.choice(self.buttons) b = random.choice(row) if b: self.callback(b)
def hook_create_menubar(self): self.mbar = Menu(self, type='menubar') self['menu'] = self.mbar # create the 'File' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='File', underline=0, menu=menu) menu.add_command(label='0pen...', underline=0, command=callit(self.dummy_callback, 'Open')) menu.add_command(label='New', underline=0, command=callit(self.dummy_callback, 'New')) menu.add_command(label='Save', underline=0, command=callit(self.dummy_callback, 'Save')) menu.add_command(label='Save As ...', underline=5, command=callit(self.dummy_callback, 'Save As')) menu.add_separator() menu.add_command(label='Print', underline=0, command=callit(self.dummy_callback, 'Print')) menu.add_command(label='Print Setup ...', underline=5, command=callit(self.dummy_callback, 'Print Setup')) menu.add_separator() menu.add_command(label='Dismiss', underline=0, command=self.dismiss_callback) # create the basic menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='Basic', underline=0, menu=menu) menu.add_command(label='Long entry which does nothing') for l in ('A', 'B', 'C', 'D', 'E', 'F'): menu.add_command(label='Print letter "%s"' % l, accelerator='%s+%s' % (ACCEL_KEY, l), command=callit(self.printletter_callback, l)) # create the 'Cascades' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='Cascades', underline=0, menu=menu) menu.add_command(label='Print Hello', accelerator='%s+%s' % (ACCEL_KEY, 'H'), command=callit(self.print_callback, "Hello!")) menu.add_command(label='Print Goodby', accelerator='%s+%s' % (ACCEL_KEY, 'G'), command=callit(self.print_callback, "Goodby!")) # add check buttons sub-menu cb_menu = Menu(menu, tearoff=0) self.vars = {} labels = ('Oil checked', 'Transmission checked', 'Brakes checked', 'Lights checked') for l in labels: self.vars[l] = IntVar(self) cb_menu.add_checkbutton(label=l, variable=self.vars[l]) cb_menu.add_separator() cb_menu.add_command(label='Show variables', command=callit(self.showvars_callback, labels)) menu.add_cascade(label='Check buttons', underline=0, menu=cb_menu) # add radio-buttons sub-menu cb_menu = Menu(menu, tearoff=0) self.vars['size'] = StringVar(self) self.vars['font'] = StringVar(self) for l in (10, 14, 18, 24, 32): cb_menu.add_radiobutton(label='l' + ' points', variable=self.vars['size']) cb_menu.add_separator() for l in ('Roman', 'Bold', 'Italic'): cb_menu.add_radiobutton(label=l, variable=self.vars['font']) cb_menu.add_separator() cb_menu.add_command(label='Show variables', command=callit(self.showvars_callback, ('size', 'font'))) menu.add_cascade(label='Radio buttons', underline=0, menu=cb_menu) # add 'Icons' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='Icons', underline=0, menu=menu) for b in ('@' + demo_path('images', 'pattern.bmp'), 'info', 'questhead', 'error'): menu.add_command(bitmap=b, hidemargin=1, command=self.icon_callback) menu.entryconfigure(2, columnbreak=1) # add 'More' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='More', underline=0, menu=menu) for l in ('An entry', 'Another entry', 'Does nothing', 'Just turn thumbs', 'Save the world', 'Armageddon'): menu.add_command(label=l, command=callit(self.youselected_callback, l)) # And, finally, add the colors menu menu = Menu(self.mbar, tearoff=1) self.mbar.add_cascade(label='Colors', underline=2, menu=menu) for l in ('red', 'green', 'blue', 'yellow', 'cyan', 'orange', 'brown'): menu.add_command(label=l, background=l, command=callit(self.color_callback, l)) # bind menu entry selection event # ?? Why it does not work for Menubuton selection ?? # (the tk demo does it) self.bind_class('Menu', '<<MenuSelect>>', self.entryselect_callback) self.bind_class('Menubutton', '<<MenuSelect>>', self.entryselect_callback)
def __init__(self): DemoWindow.__init__(self, '', demo_path('twind.py')) frame = Frame(self) frame.pack(expand=Y, fill=BOTH) self.frame = frame frame.rowconfigure(0, weight=1) frame.columnconfigure(0, weight=1) self.text = Text(frame, relief=SUNKEN, bd=2, setgrid=1, wrap='word', height=35) self.text.grid(row=0, column=0, sticky='nsew') bar = Scrollbar(frame) bar.grid(row=0, column=1, sticky='nsew') self.text['yscrollcommand'] = bar.set bar['command'] = self.text.yview self.hscroll = None # preset is needed because it is checked in the self.plot = None # callbacks #these are the buttons which will be embedded in the text self.b_turnon = Button(self.text, text='Turn On', cursor='top_left_arrow', command=self.turnon_callback) self.b_turnoff = Button(self.text, text='Turn Off', cursor='top_left_arrow', command=self.turnoff_callback) self.b_clickhere = Button(self.text, text='Click Here', cursor='top_left_arrow', command=self.clickhere_callback) self.b_delete = Button(self.text, text='Delete', cursor='top_left_arrow', command=self.delete_callback) # insert text and embedding buttons self.text.insert( END, "A text widget can contain other widgets embedded " + "in it. These are called \"embedded windows\", " + "and they can consist of arbitrary widgets. " + "For example, here are two embedded button " + "widgets. You can click on the first button to ") self.text.window_create(END, window=self.b_turnon) self.text.insert( END, " horizontal scrolling, which also turns off " + "word wrapping. Or, you can click on the second " + "button to ") self.text.window_create(END, window=self.b_turnoff) self.text.insert( END, "horizontal scrolling and turn back on word wrapping.\n\n") self.text.insert(END, "Or, here is another example. If you ") self.text.window_create(END, window=self.b_clickhere) self.text.insert( END, "a canvas displaying an x-y plot will appear right " + "here.") # insert a mark where the plot window shall be inserted self.text.mark_set('plot', INSERT) self.text.mark_gravity('plot', LEFT) self.text.insert( END, "\nYou can drag the data points around with " + "the mouse, or you can click here to ") self.text.window_create(END, window=self.b_delete) self.text.insert(END, " the plot again.\n\n") self.text.insert( END, "You may also find it useful to put embedded windows in " "a text without any actual text. In this case the " + "text widget acts like a geometry manager. For " + "example, here is a collection of buttons laid out " + "neatly into rows by the text widget. These buttons " + "can be used to change the background color of the " + "text widget (\"Default\" restores the color to " + "its default). If you click on the button labeled " + "\"Short\", it changes to a longer string so that " + "you can see how the text widget automatically " + "changes the layout. Click on the button again " + "to restore the short string.\n") self.var = StringVar(self.text) self.var.set('Short') tb = Checkbutton(self.text, indicatoron=0, textvariable=self.var, variable=self.var, offvalue='Short', onvalue='A much longer string', cursor='top_left_arrow', padx=2, pady=4) self.text.window_create(END, window=tb) colors = ('AntiqueWhite3', 'Bisque1', 'Bisque2', 'Bisque3', 'Bisque4', 'SlateBlue3', 'RoyalBlue1', 'SteelBlue2', 'DeepSkyBlue3', 'LightBlue1', 'DarkSlateGray1', 'Aquamarine2', 'DarkSeaGreen2', 'SeaGreen1', 'Yellow1', 'IndianRed1', 'IndianRed2', 'Tan1', 'Tan4') c = self.text['background'] db = Button(self.text, text='Default', background=c, command=callit(self.button_callback, c), cursor='top_left_arrow') self.text.window_create(END, window=db) for c in colors: b = Button(self.text, text=c, background=c, padx=2, pady=4, cursor='top_left_arrow', command=callit(self.button_callback, c)) self.text.window_create(END, window=b)
def hook_create_menubar(self): self.mbar = Menu(self, type='menubar') self['menu'] = self.mbar # create the 'File' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='File', underline=0, menu=menu ) menu.add_command(label='0pen...', underline=0, command=callit(self.dummy_callback,'Open')) menu.add_command(label='New', underline=0, command=callit(self.dummy_callback,'New')) menu.add_command(label='Save', underline=0, command=callit(self.dummy_callback,'Save')) menu.add_command(label='Save As ...', underline=5, command=callit(self.dummy_callback,'Save As')) menu.add_separator() menu.add_command(label='Print', underline=0, command=callit(self.dummy_callback,'Print')) menu.add_command(label='Print Setup ...', underline=5, command=callit(self.dummy_callback,'Print Setup')) menu.add_separator() menu.add_command(label='Dismiss', underline=0, command=self.dismiss_callback) # create the basic menu menu = Menu(self.mbar, tearoff=0 ) self.mbar.add_cascade(label='Basic', underline=0, menu=menu) menu.add_command(label='Long entry which does nothing') for l in ('A','B','C','D','E', 'F'): menu.add_command(label='Print letter "%s"'%l, accelerator='%s+%s'%(ACCEL_KEY,l), command=callit(self.printletter_callback, l )) # create the 'Cascades' menu menu = Menu(self.mbar, tearoff=0 ) self.mbar.add_cascade(label='Cascades', underline=0, menu=menu) menu.add_command(label='Print Hello', accelerator='%s+%s'%(ACCEL_KEY,'H'), command=callit(self.print_callback, "Hello!")) menu.add_command(label='Print Goodby', accelerator='%s+%s'%(ACCEL_KEY,'G'), command=callit(self.print_callback, "Goodby!")) # add check buttons sub-menu cb_menu = Menu(menu,tearoff=0) self.vars={} labels = ('Oil checked', 'Transmission checked', 'Brakes checked', 'Lights checked' ) for l in labels: self.vars[l]=IntVar(self) cb_menu.add_checkbutton(label=l, variable=self.vars[l] ) cb_menu.add_separator() cb_menu.add_command(label='Show variables', command=callit(self.showvars_callback, labels ) ) menu.add_cascade(label='Check buttons', underline=0, menu=cb_menu) # add radio-buttons sub-menu cb_menu=Menu(menu,tearoff=0) self.vars['size'] = StringVar(self) self.vars['font'] = StringVar(self) for l in (10,14,18,24,32): cb_menu.add_radiobutton(label='l'+' points', variable=self.vars['size'] ) cb_menu.add_separator() for l in ('Roman','Bold','Italic'): cb_menu.add_radiobutton(label=l, variable=self.vars['font'] ) cb_menu.add_separator() cb_menu.add_command(label='Show variables', command=callit(self.showvars_callback, ('size', 'font') )) menu.add_cascade(label='Radio buttons', underline=0, menu=cb_menu) # add 'Icons' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='Icons', underline=0, menu=menu) for b in ( '@'+demo_path('images','pattern.bmp'), 'info', 'questhead', 'error' ): menu.add_command(bitmap=b, hidemargin=1, command=self.icon_callback ) menu.entryconfigure(2,columnbreak=1) # add 'More' menu menu = Menu(self.mbar, tearoff=0) self.mbar.add_cascade(label='More', underline=0, menu=menu) for l in ( 'An entry', 'Another entry', 'Does nothing', 'Just turn thumbs' , 'Save the world', 'Armageddon'): menu.add_command(label=l, command=callit(self.youselected_callback,l)) # And, finally, add the colors menu menu = Menu(self.mbar, tearoff=1) self.mbar.add_cascade(label='Colors', underline=2, menu=menu) for l in ( 'red','green', 'blue', 'yellow', 'cyan', 'orange', 'brown'): menu.add_command(label=l, background=l, command=callit(self.color_callback,l)) # bind menu entry selection event # ?? Why it does not work for Menubuton selection ?? # (the tk demo does it) self.bind_class('Menu', '<<MenuSelect>>', self.entryselect_callback ) self.bind_class('Menubutton', '<<MenuSelect>>', self.entryselect_callback )
def __init__(self): DemoWindow.__init__(self, '', demo_path('twind.py') ) frame = Frame(self); frame.pack(expand=Y, fill=BOTH ) self.frame=frame frame.rowconfigure(0, weight=1 ) frame.columnconfigure(0, weight=1 ) self.text=Text(frame, relief=SUNKEN, bd=2, setgrid=1, wrap='word', height=35); self.text.grid(row=0,column=0, sticky='nsew') bar=Scrollbar(frame); bar.grid(row=0,column=1,sticky='nsew') self.text['yscrollcommand']=bar.set; bar['command']=self.text.yview self.hscroll = None # preset is needed because it is checked in the self.plot = None # callbacks #these are the buttons which will be embedded in the text self.b_turnon = Button(self.text, text='Turn On', cursor='top_left_arrow', command = self.turnon_callback ) self.b_turnoff = Button( self.text, text='Turn Off', cursor='top_left_arrow', command=self.turnoff_callback ) self.b_clickhere = Button(self.text, text='Click Here', cursor='top_left_arrow', command=self.clickhere_callback ) self.b_delete = Button( self.text,text='Delete', cursor='top_left_arrow', command=self.delete_callback ) # insert text and embedding buttons self.text.insert(END, "A text widget can contain other widgets embedded "+ "in it. These are called \"embedded windows\", "+ "and they can consist of arbitrary widgets. "+ "For example, here are two embedded button "+ "widgets. You can click on the first button to " ) self.text.window_create(END, window=self.b_turnon ) self.text.insert(END, " horizontal scrolling, which also turns off "+ "word wrapping. Or, you can click on the second "+ "button to " ) self.text.window_create(END, window=self.b_turnoff ) self.text.insert(END, "horizontal scrolling and turn back on word wrapping.\n\n" ) self.text.insert(END, "Or, here is another example. If you ") self.text.window_create(END, window=self.b_clickhere ) self.text.insert(END, "a canvas displaying an x-y plot will appear right "+ "here." ) # insert a mark where the plot window shall be inserted self.text.mark_set('plot', INSERT ) self.text.mark_gravity('plot', LEFT) self.text.insert(END, "\nYou can drag the data points around with "+ "the mouse, or you can click here to ") self.text.window_create(END, window=self.b_delete ) self.text.insert(END, " the plot again.\n\n") self.text.insert( END, "You may also find it useful to put embedded windows in " "a text without any actual text. In this case the "+ "text widget acts like a geometry manager. For "+ "example, here is a collection of buttons laid out "+ "neatly into rows by the text widget. These buttons "+ "can be used to change the background color of the "+ "text widget (\"Default\" restores the color to "+ "its default). If you click on the button labeled "+ "\"Short\", it changes to a longer string so that "+ "you can see how the text widget automatically "+ "changes the layout. Click on the button again "+ "to restore the short string.\n" ) self.var = StringVar(self.text) self.var.set('Short') tb = Checkbutton( self.text, indicatoron=0, textvariable=self.var, variable=self.var, offvalue='Short', onvalue='A much longer string', cursor='top_left_arrow', padx=2, pady=4) self.text.window_create(END, window=tb ) colors = ( 'AntiqueWhite3', 'Bisque1', 'Bisque2', 'Bisque3', 'Bisque4', 'SlateBlue3', 'RoyalBlue1', 'SteelBlue2', 'DeepSkyBlue3', 'LightBlue1', 'DarkSlateGray1', 'Aquamarine2', 'DarkSeaGreen2', 'SeaGreen1', 'Yellow1', 'IndianRed1', 'IndianRed2', 'Tan1', 'Tan4' ) c = self.text['background'] db = Button(self.text, text='Default', background=c, command=callit(self.button_callback, c ), cursor = 'top_left_arrow' ) self.text.window_create(END, window=db) for c in colors: b = Button(self.text, text=c, background=c, padx=2, pady=4, cursor='top_left_arrow', command=callit(self.button_callback, c )) self.text.window_create(END, window=b)