### Example of element_options output Source: https://tkdocs.com/shipman/ttk-layouts.html Continues the conversational example, showing the result of calling element_options on the 'Button.highlight' style. ```python >>> d = s.element_options('Button.highlight') >>> d ('-highlightcolor', '-highlightthickness') ``` -------------------------------- ### Install Ruby with Homebrew Source: https://tkdocs.com/tutorial/install.html Installs Ruby using Homebrew. The initial '%' is the Unix shell prompt and is not typed. After installation, Ruby binaries are placed in /usr/local/opt/ruby/bin. ```bash % brew install ruby ``` -------------------------------- ### Install Ruby/Tk gem on Windows Source: https://tkdocs.com/tutorial/install.html Run this command in a command prompt to install the Ruby/Tk gem. The initial '%' is the Unix shell prompt and is not typed. ```ruby % gem install tk ``` -------------------------------- ### Build and install Tcl/Tk from source Source: https://tkdocs.com/tutorial/install.html Builds and installs Tcl and Tk from source into the current directory. Adjust version numbers (e.g., 8.6.17) as needed; change the `prefix` option to install elsewhere. ```shell % export TCLTKDIR=`pwd` % cd tcl8.6.17/unix % ./configure --prefix=\$TCLTKDIR % make && make install % cd ../../tk8.6.17/unix % ./configure --with-tcl=\$TCLTKDIR/lib % make && make install ``` -------------------------------- ### Sketchpad color selection with item bindings in Tcl/Tk (full example) Source: https://tkdocs.com/tutorial/canvas.html Complete Tcl/Tk example for a sketchpad that changes drawing color when colored rectangles are clicked. Includes setColor and addLine procedures. ```tcl set id [.canvas create rectangle 10 10 30 30 -fill red] .canvas bind $id <1> "setColor red" set id [.canvas create rectangle 10 35 30 55 -fill blue] .canvas bind $id <1> "setColor blue" set id [.canvas create rectangle 10 60 30 80 -fill black] .canvas bind $id <1> "setColor black" set ::color black proc setColor {color} { set ::color $color } proc addLine {x y} { .canvas create line $::lastx $::lasty $x $y -fill $::color set ::lastx $x; set ::lasty $y } ``` -------------------------------- ### Tcl/Tk grid padding example Source: https://tkdocs.com/tutorial/grid.html Tcl/Tk equivalent of the grid padding example, using ttk::frame, ttk::checkbutton, and grid commands with padx/pady and column/row weights. ```tcl ttk::frame .c -padding "3 3 12 12" ttk::frame .c.f -borderwidth 5 -relief ridge -width 200 -height 100 ttk::label .c.namelbl -text Name ttk::entry .c.name ttk::checkbutton .c.one -text One -variable one -onvalue 1; set one 1 ttk::checkbutton .c.two -text Two -variable two -onvalue 1; set two 0 ttk::checkbutton .c.three -text Three -variable three -onvalue 1; set three 1 ttk::button .c.ok -text Okay ttk::button .c.cancel -text Cancel grid .c -column 0 -row 0 -sticky nsew grid .c.f -column 0 -row 0 -columnspan 3 -rowspan 2 -sticky nsew grid .c.namelbl -column 3 -row 0 -columnspan 2 -sticky nw -padx 5 grid .c.name -column 3 -row 1 -columnspan 2 -sticky new -pady 5 -padx 5 grid .c.one -column 0 -row 5 grid .c.two -column 1 -row 5 grid .c.three -column 2 -row 5 grid .c.ok -column 3 -row 5 grid .c.cancel -column 4 -row 5 grid columnconfigure . 0 -weight 1 grid rowconfigure . 0 -weight 1 grid columnconfigure .c 0 -weight 3 grid columnconfigure .c 1 -weight 3 grid columnconfigure .c 2 -weight 3 grid columnconfigure .c 3 -weight 1 grid columnconfigure .c 4 -weight 1 grid rowconfigure .c 1 -weight 1 ``` -------------------------------- ### Install Ruby/Tk gem Source: https://tkdocs.com/tutorial/install.html Install the Ruby/Tk binding gem after installing the development packages. ```shell % sudo gem install tk ``` -------------------------------- ### BitmapImage usage example Source: https://tkdocs.com/shipman/images.html Example of creating a BitmapImage and displaying it in a Label widget. The image object is passed to the image option of the Label. ```python logo = tk.BitmapImage('logo.xbm', foreground='red') Label(image=logo).grid() ``` -------------------------------- ### Install Tcl/Tk with Homebrew Source: https://tkdocs.com/tutorial/install.html Installs Tcl/Tk via Homebrew. This is the recommended easy way to get a working Tcl/Tk for Tkinter on macOS, avoiding the buggy system version (8.5.9). ```bash % brew install tcl-tk ``` -------------------------------- ### Event handler example: __drawOrangeBlob Source: https://tkdocs.com/shipman/event-handlers.html Example event handler that draws an orange blob on a canvas at the mouse position. Uses the event.x and event.y attributes to get the mouse coordinates relative to the widget. The handler is bound to mouse button 2 clicks on a canvas named self.canv. ```python def __drawOrangeBlob(self, event): '''Draws an orange blob in self.canv where the mouse is. ''' r = 5 # Blob radius self.canv.create_oval(event.x-r, event.y-r, event.x+r, event.y+r, fill='orange') ``` -------------------------------- ### Create an OptionMenu with a list of choices Source: https://tkdocs.com/shipman/optionmenu.html Example used to create the illustration above. Sets the initial choice to the first item in the list and creates an OptionMenu with the given choices. ```python optionList = ('train', 'plane', 'boat') self.v = tk.StringVar() self.v.set(optionList[0]) self.om = tk.OptionMenu(self, self.v, *optionList) ``` -------------------------------- ### Get element options with Style.element_options() Source: https://tkdocs.com/shipman/ttk-layouts.html Returns a sequence of option strings for a given style name, each preceded by a hyphen. For example, element_options('Button.highlight') returns ('-highlightcolor', '-highlightthickness'). ```python _S_.element_options(_styleName_) ``` -------------------------------- ### Create an arc item (Tkinter) Source: https://tkdocs.com/tutorial/canvas.html Creates an arc item. The start, extent, and style attributes control the arc's appearance. The example uses a pieslice style with a yellow fill and black outline. ```python canvas.create_arc(10, 10, 200, 150, fill='yellow', outline='black', start=45, extent=135, width=5) ``` -------------------------------- ### Interactive Tkinter configuration example Source: https://tkdocs.com/tutorial/concepts.html Shows how to create a button with options, retrieve and change the text option, and inspect all configuration options using the configure method. Includes both Python and Tcl/Tk versions. ```python % python >>> from tkinter import * >>> from tkinter import ttk >>> root = Tk() # create a button, passing two options: >>> button = ttk.Button(root, text="Hello", command="buttonpressed") >>> button.grid() #check the current value of the text option: >>> button['text'] 'Hello' # change the value of the text option: >>> button['text'] = 'goodbye' # another way to do the same thing: >>> button.configure(text='goodbye') # check the current value of the text option: >>> button['text'] 'goodbye' # get all information about the text option: >>> button.configure('text') ('text', 'text', 'Text', '', 'goodbye') # get information on all options for this widget: >>> button.configure() {'cursor': ('cursor', 'cursor', 'Cursor', '', ''), 'style': ('style', 'style', 'Style', '', ''), 'default': ('default', 'default', 'Default', , ), 'text': ('text', 'text', 'Text', '', 'goodbye'), 'image': ('image', 'image', 'Image', '', ''), 'class': ('class', '', '', '', ''), 'padding': ('padding', 'padding', 'Pad', '', ''), 'width': ('width', 'width', 'Width', '', ''), 'state': ('state', 'state', 'State', , ), 'command': ('command', 'command' , 'Command', '', 'buttonpressed'), 'textvariable': ('textvariable', 'textVariable', 'Variable', '', ''), 'compound': ('compound', 'compound', 'Compound', , ), 'underline': ('underline', 'underline', 'Underline', -1, -1), 'takefocus': ('takefocus', 'takeFocus', 'TakeFocus', '', 'ttk::takefocus')} ``` ```tcl % **wish8.5** _create a button, passing two options:_ % **grid [ttk::button .b -text "Hello" -command {button_pressed}]** _check the current value of the text option:_ % **.b cget -text** Hello _check the current value of the command option:_ % **.b cget -command** button_pressed _change the value of the text option:_ % **.b configure -text Goodbye** _check the current value of the text option:_ % **.b cget -text** Goodbye _get all information about the text option:_ % **.b configure -text** -text text Text {} Goodbye _get information on all options for this widget:_ % **.b configure** {-takefocus takeFocus TakeFocus ttk::takefocus ttk::takefocus} {-command command Command {} button_pressed} {-default default Default normal normal} {-text text Text {} Goodbye} {-textvariable textVariable Variable {} {}} {-underline underline Underline -1 -1} {-width width Width {} {}} {-image image Image {} {}} {-compound compound Compound none none} {-padding padding Pad {} {}} {-state state State normal normal} {-takefocus takeFocus TakeFocus {} ttk::takefocus} {-cursor cursor Cursor {} {}} {-style style Style {} {}} {-class {} {} {} {}} ``` -------------------------------- ### Hello World in Tkinter (Python) Source: https://tkdocs.com/tutorial/install.html Create a file named hello.py with this Tkinter program and run it from a command prompt with 'python hello.py'. If the file is not found, provide the full path to hello.py. ```python from tkinter import * from tkinter import ttk root = Tk() ttk.Button(root, text="Hello World").grid() root.mainloop() ``` ```shell % python hello.py ``` -------------------------------- ### Get RGB values for a color with winfo_rgb Source: https://tkdocs.com/tutorial/fonts.html Retrieves the RGB values (each between 0 and 65535) for a named color using the winfo_rgb method on any widget. The examples show the same call in Python, Tcl, Ruby, and Perl. ```python root.winfo_rgb('red') ``` ```tcl winfo rgb . red ``` ```ruby root.winfo_rgb('red') ``` ```perl $mw->g_winfo_rgb('red') ``` -------------------------------- ### Retrieve widget class with winfo_class() Source: https://tkdocs.com/shipman/ttk-style-layer.html Shows how to get the widget class of a ttk widget at runtime using the .winfo_class() method. The example creates a Button and a Treeview, then prints their class names and the Python class of the button. ```python >>> b=ttk.Button(None) >>> b.winfo_class() 'TButton' >>> t=ttk.Treeview(None) >>> t.winfo_class() 'Treeview' >>> b.__class__ # Here, we are asking for the Python class ``` -------------------------------- ### Ruby: Creating a root, frame, and button Source: https://tkdocs.com/tutorial/concepts.html Demonstrates creating a Tk root window, a frame, and a button in Ruby. The root window is the only widget without a parent; all other widgets must be passed their parent as a parameter to the widget class' new method. ```ruby root = TkRoot.new content = Tk::Tile::Frame.new(root) Tk::Tile::Button.new(content) ``` -------------------------------- ### Get font actual, metrics, and measure for TkTextFont Source: https://tkdocs.com/tutorial/fonts.html Inspect a named font's actual attributes, metrics, and text measurement. The Python example uses `font.nametofont('TkTextFont')`; the Tcl, Ruby, and Perl examples show the equivalent commands. The output shown is from a macOS system (family `.AppleSystemUIFont`). You must initialize Tk before using these functions, even in an interactive shell. ```python >>> from tkinter import font >>> f = font.nametofont('TkTextFont') >>> f.actual() {'family': '.AppleSystemUIFont', 'size': 13, 'weight': 'normal', 'slant': 'roman', 'underline': 0, 'overstrike': 0} >>> f.metrics() {'ascent': 13, 'descent': 3, 'linespace': 16, 'fixed': 0} >>> f.measure('The quick brown fox') 124 ``` ```tcl % font actual TkTextFont -family .AppleSystemUIFont -size 13 -weight normal -slant roman -underline 0 -overstrike 0 % font metrics TkTextFont -ascent 13 -descent 3 -linespace 16 -fixed 0 % font measure TkTextFont "The quick brown fox" 124 ``` ```ruby irb(main):003:0> TkFont.actual "TkTextFont" => [["family", ".AppleSystemUIFont"], ["size", 13], ["weight", "normal"], ["slant", "roman"], ["underline", false], ["overstrike", false]] irb(main):004:0> TkFont.metrics "TkTextFont" => [["ascent", 13], ["descent", 3], ["linespace", 16], ["fixed", 0]] irb(main):005:0> TkFont.measure "TkTextFont", "The quick brown fox" => 124 ``` ```perl perl> Tkx::font_actual('TkTextFont') -family .AppleSystemUIFont -size 13 -weight normal -slant roman -underline 0 -overstrike 0 perl> Tkx::font_metrics('TkTextFont') -ascent 13 -descent 3 -linespace 16 -fixed 0 perl> Tkx::font_measure('TkTextFont', 'The quick brown fox') 124 ``` -------------------------------- ### Python/Tkinter sketchpad with global variables Source: https://tkdocs.com/tutorial/canvas.html Procedural Python/Tkinter example that stores the last mouse position in global variables and creates a line on each mouse drag. Bind to save the start position and to draw a line from the last position to the current one. ```python from tkinter import * from tkinter import ttk def savePosn(event): global lastx, lasty lastx, lasty = event.x, event.y def addLine(event): canvas.create_line(lastx, lasty, event.x, event.y) savePosn(event) root = Tk() root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) canvas = Canvas(root) canvas.grid(column=0, row=0, sticky=(N, W, E, S)) canvas.bind("", savePosn) canvas.bind("", addLine) root.mainloop() ``` -------------------------------- ### Initialize country data and create main frame with Tkx in Perl Source: https://tkdocs.com/tutorial/morewidgets.html Initializes country data (codes, names, populations) and gift names, then creates the main window and a ttk frame with padding, gridding it with sticky 'nwes' and configuring column/row weights for resizing. ```perl use Tkx; # Initialize our country "databases": # - the list of country codes (a subset anyway) # - parallel list of country names, same order as the country codes # - a hash table mapping country code to population @countrycodes = ("ar", "au", "be", "br", "ca", "cn", "dk", "fi", "fr", "gr", "in", "it", "jp", "mx", "nl", "no", "es", "se", "ch"); @countrynames = ("Argentina", "Australia", "Belgium", "Brazil", "Canada", "China", "Denmark", "Finland", "France", "Greece", "India", "Italy", "Japan", "Mexico", "Netherlands", "Norway", "Spain", "Sweden", "Switzerland"); %populations = ("ar" => 41000000, "au" => 21179211, "be" => 10584534, "br" => 185971537, "ca" => 33148682, "cn" => 1323128240, "dk" => 5457415, "fi" => 5302000, "fr" => 64102140, "gr" => 11147000, "in" => 1131043000, "it" => 59206382, "jp" => 127718000, "mx" => 106535000, "nl" => 16402414, "no" => 4738085, "es" => 45116894, "se" => 9174082, "ch" => 7508700); # Names of the gifts we can send %gifts =("card" => "Greeting card", "flowers" => "Flowers", "nastygram" => "Nastygram"); # Create and grid the outer content frame $mw = Tkx::widget->new("."); $content = $mw->new_ttk__frame(-padding => "5 5 12 0"); $content->g_grid(-column => 0, -row => 0, -sticky => "nwes"); $mw->g_grid_columnconfigure(0, -weight => 1); $mw->g_grid_rowconfigure(0, -weight => 1); ``` -------------------------------- ### Set and get treeview item values (Python, Tcl, Ruby, Perl) Source: https://tkdocs.com/tutorial/tree.html Set and retrieve a value for a specific item and column, and insert an item with values for all columns. The order of the values list must match the 'columns' configuration option. The same example is shown in Python, Tcl, Ruby, and Perl. ```python tree.set('widgets', 'size', '12KB') size = tree.set('widgets', 'size') tree.insert('', 'end', text='Listbox', values=('15KB', 'Yesterday', 'mark')) ``` ```tcl .tree set widgets size "12KB" set size [.tree set widgets size] .tree insert "" end -text Listbox -values [list "15KB" "Yesterday" "mark"] ``` ```ruby tree.set('widgets', 'size', '12KB'); # or item.set('size', '12KB') size = tree.get('widgets', 'size'); # or item.get('size') tree.insert('', 'end', :text => 'Listbox', :values => ['15KB','Yesterday','mark']) ``` ```perl $tree->set("widgets", "size", "12KB"); $size = $tree->set("widgets", "size"); $tree->insert("", "end", -text => "Listbox", -values => "15KB Yesterday mark"); ``` -------------------------------- ### Get maximum window size spanning multiple monitors with maxsize Source: https://tkdocs.com/tutorial/windows.html Find the approximate maximum size of the entire virtual display spanning multiple monitors by checking a toplevel widget's maximum size. The reported size may be slightly smaller than the full display, for example reduced by the macOS menubar. ```python root.maxsize() ``` ```tcl wm maxsize . ``` ```ruby root.wm_maxsize ``` ```perl $mw->g_wm_maxsize() ``` -------------------------------- ### Feet to Meters - Object Oriented Style (Perl) Source: https://tkdocs.com/tutorial/concepts.html The feet-to-meters example rewritten using Tkx's object-oriented interface. It creates the main window, a frame, entry, label, and button widgets, and arranges them with grid geometry. The calculate subroutine converts feet to meters and the main loop is started with Tkx::MainLoop(). ```perl use Tkx; my $mw = Tkx::widget->new("."); $mw->g_wm_title("Feet to Meters"); my $frm = $mw->new_ttk__frame(-padding => "3 3 12 12"); $frm->g_grid(-column => 0, -row => 0, -sticky => "nwes"); $mw->g_grid_columnconfigure(0, -weight => 1); $mw->g_grid_rowconfigure(0, -weight => 1); my $ef = $frm->new_ttk__entry(-width => 7, -textvariable => \$feet); $ef->g_grid(-column => 2, -row => 1, -sticky => "we"); my $em = $frm->new_ttk__label(-textvariable => \$meters); $em->g_grid(-column => 2, -row => 2, -sticky => "we"); my $cb = $frm->new_ttk__button(-text => "Calculate", -command => sub {calculate();}); $cb->g_grid(-column => 3, -row => 3, -sticky => "w"); $frm->new_ttk__label(-text => "feet")->g_grid(-column => 3, -row => 1, -sticky => "w"); $frm->new_ttk__label(-text => "is equivalent to")->g_grid(-column => 1, -row => 2, -sticky => "e"); $frm->new_ttk__label(-text => "meters")->g_grid(-column => 3, -row => 2, -sticky => "w"); foreach (Tkx::SplitList($frm->g_winfo_children)) { Tkx::grid_configure($_, -padx => 5, -pady => 5); } $ef->g_focus; $mw->g_bind("", sub {calculate();}); sub calculate { $meters = int(0.3048*$feet*10000.0+.5)/10000.0 || ''; } Tkx::MainLoop(); ``` -------------------------------- ### Hello World in Ruby/Tk Source: https://tkdocs.com/tutorial/install.html Save this Ruby/Tk code to a file named hello.rb and run it from irb with 'source "hello.rb"'. If the file is not found, provide the full path or use Ruby's Dir.pwd and Dir.chdir commands. ```ruby require 'tk' require 'tkextlib/tile' root = TkRoot.new() button = Tk::Tile::TButton.new(root) {text "Hello World"}.grid Tk.mainloop() ``` ```shell % source "hello.rb" ``` -------------------------------- ### Get screen color depth, pixel density, and resolution with winfo Source: https://tkdocs.com/tutorial/windows.html Query the display's color depth, color model, pixel density, and resolution using the winfo command. The Python example uses root.winfo_screendepth(), root.winfo_screenvisual(), root.winfo_pixels('1i'), root.winfo_screenwidth(), and root.winfo_screenheight(); the Tcl, Ruby, and Perl equivalents use the corresponding winfo subcommands. ```python print(f"color depth={root.winfo_screendepth()} ({root.winfo_screenvisual()})") print(f"pixels per inch={root.winfo_pixels('1i')}") print(f"width={root.winfo_screenwidth()} height={root.winfo_screenheight()}") ``` ```tcl puts "color depth=[winfo screendepth .] ([winfo screenvisual .])" puts "pixels per inch=[winfo pixels . 1i]" puts "width=[winfo screenwidth .] height=[winfo screenheight .]" ``` ```ruby print "color depth=" + root.winfo_screendepth().to_s + " (" + root.winfo_screenvisual() + ")\n" print "pixels per inch=" + root.winfo_pixels('1i').to_s + "\n" print "width=" + root.winfo_screenwidth().to_s + " height=" + root.winfo_screenheight().to_s + "\n" ``` ```perl print("color depth=", $mw->g_winfo_screendepth(), " (", $mw->g_winfo_screenvisual(), ")\n"); print("pixels per inch=", $mw->g_winfo_pixels("1i"), "\n"); print("width=", $mw->g_winfo_screenwidth(), " height=", $mw->g_winfo_screenheight(), "\n"); ``` -------------------------------- ### Create a notebook and add pages in Perl/Tk Source: https://tkdocs.com/tutorial/complex.html Creates a notebook using new_ttk__notebook (a.k.a. Tkx::ttk__notebook) and adds two frames as pages with text labels. Each page must be a direct child of the notebook. ```Perl/Tk $n = $mw->new_ttk__notebook; $f1 = $n->new_ttk__frame; # first page, which would get widgets gridded into it $f2 = $n->new_ttk__frame; # second page $n->add($f1, -text => "One"); $n->add($f2, -text => "Two"); ``` -------------------------------- ### Install Ruby/Tk gem Source: https://tkdocs.com/tutorial/install.html Installs the Ruby/Tk gem. The Tk gem looks for installed versions of Tcl and Tk in /Library/Frameworks, where ActiveTcl places them. ```bash % /usr/local/opt/ruby/bin/gem install tk ``` -------------------------------- ### Perl/Tkx grid layout with padding and resize weights Source: https://tkdocs.com/tutorial/grid.html Perl/Tkx equivalent of the grid layout example, using Tkx::widget methods and g_grid for geometry management. It demonstrates padding, column/row spans, sticky options, and weight configuration for resize behavior. ```perl use Tkx; my $mw = Tkx::widget->new("."); my $content = $mw->new_ttk__frame(-padding => "3 3 12 12"); my $frame = $content->new_ttk__frame(-borderwidth => 5, -relief => "ridge", -width => 200, -height => 100); my $namelbl = $content->new_ttk__label(-text => "Name"); my $name = $content->new_ttk__entry; $option_one = 1; $option_two = 0; $option_three = 1; my $one = $content->new_ttk__checkbutton(-text => "One", -variable => \$option_one, -onvalue => 1); my $two = $content->new_ttk__checkbutton(-text => "Two", -variable => \$option_two, -onvalue => 1); my $three = $content->new_ttk__checkbutton(-text => "Three", -variable => \$option_three, -onvalue => 1); my $ok = $content->new_ttk__button(-text => "Okay"); my $cancel = $content->new_ttk__button(-text => "Cancel"); $content->g_grid(-column => 0, -row => 0, -sticky => "nsew"); $frame->g_grid(-column => 0, -row => 0, -columnspan => 3, -rowspan => 2, -sticky => "nsew"); $namelbl->g_grid(-column => 3, -row => 0, -columnspan => 2, -sticky => "nw", -padx => 5); $name->g_grid(-column => 3, -row => 1, -columnspan => 2, -sticky => "new", -pady => 5, -padx => 5); $one->g_grid(-column => 0, -row => 5); $two->g_grid(-column => 1, -row => 5); $three->g_grid(-column => 2, -row => 5); $ok->g_grid(-column => 3, -row => 5); $cancel->g_grid(-column => 4, -row => 5); $mw->g_grid_columnconfigure(0, -weight => 1); $mw->g_grid_rowconfigure(0, -weight => 1); $content->g_grid_columnconfigure(0, -weight => 3); $content->g_grid_columnconfigure(1, -weight => 3); $content->g_grid_columnconfigure(2, -weight => 3); $content->g_grid_columnconfigure(3, -weight => 1); $content->g_grid_columnconfigure(4, -weight => 1); $content->g_grid_rowconfigure(1, -weight => 1); Tkx::MainLoop; ``` -------------------------------- ### Ruby/Tk grid layout with padding and resize weights Source: https://tkdocs.com/tutorial/grid.html Ruby/Tk example using the grid geometry manager to lay out a form with padding, column/row spans, and weight configuration for resize behavior. It creates a frame with padding "3 3 12 12", places widgets with sticky and padx/pady options, and sets column/row weights to control how extra space is distributed. ```ruby require 'tk' require 'tkextlib/tile' root = TkRoot.new content = Tk::Tile::Frame.new(root) {padding "3 3 12 12"} frame = Tk::Tile::Frame.new(content) {borderwidth 5; relief "ridge"; width 200; height 100} namelbl = Tk::Tile::Label.new(content) {text "Name"} name = Tk::Tile::Entry.new(content) $option_one = TkVariable.new( 1 ) one = Tk::Tile::CheckButton.new(content) {text "One"; variable $option_one; onvalue 1} $option_two = TkVariable.new( 0 ) two = Tk::Tile::CheckButton.new(content) {text "Two"; variable $option_two; onvalue 1} $option_three = TkVariable.new( 1 ) three = Tk::Tile::CheckButton.new(content) {text "Three"; variable $option_three; onvalue 1} ok = Tk::Tile::Button.new(content) {text "Okay"} cancel = Tk::Tile::Button.new(content) {text "Cancel"} content.grid :column => 0, :row => 0, :sticky => 'nsew' frame.grid :column => 0, :row => 0, :columnspan => 3, :rowspan => 2, :sticky => 'nsew' namelbl.grid :column => 3, :row => 0, :columnspan => 2, :sticky => 'nw', :padx => 5 name.grid :column => 3, :row => 1, :columnspan => 2, :sticky => 'new', :pady => 5, :padx => 5 one.grid :column => 0, :row => 5 two.grid :column => 1, :row => 5 three.grid :column => 2, :row => 5 ok.grid :column => 3, :row => 5 cancel.grid :column => 4, :row => 5 TkGrid.columnconfigure( root, 0, :weight => 1 ) TkGrid.rowconfigure( root, 0, :weight => 1 ) TkGrid.columnconfigure( content, 0, :weight => 3 ) TkGrid.columnconfigure( content, 1, :weight => 3 ) TkGrid.columnconfigure( content, 2, :weight => 3 ) TkGrid.columnconfigure( content, 3, :weight => 1 ) TkGrid.columnconfigure( content, 4, :weight => 1 ) TkGrid.rowconfigure( content, 1, :weight => 1) Tk.mainloop ``` -------------------------------- ### Install Perl with Homebrew Source: https://tkdocs.com/tutorial/install.html Installs Perl using the Homebrew package manager. The initial '%' is the Unix shell prompt and should not be typed. After installation, the Perl binaries are placed in /usr/local/opt/perl/bin. ```shell % brew install perl ``` -------------------------------- ### Perl/Tk: Using tags with canvas items Source: https://tkdocs.com/tutorial/canvas.html Demonstrates creating a canvas, adding items with tags, adding and removing tags, querying tags, and finding items by tag in Perl/Tk. The example shows the output of each command, including the tag lists and item ids. ```perl Perl> **$c = $mw->new_canvas()** .c Perl> **$c->create_line(10, 10, 20, 20, -tags => "firstline drawing")** 1 Perl> **$c->create_rectangle(30,30,40,40, -tags => "drawing")** 2 Perl> **$c->addtag("rectangle", "withtag", 2)** Perl> **$c->addtag("polygon", "withtag", "rectangle")** Perl> **$c->gettags(2)** drawing rectangle polygon Perl> **$c->dtag(2,"polygon")** Perl> **$c->gettags(2)** drawing rectangle Perl> **$c->find_withtag("drawing")** 1 2 ``` -------------------------------- ### Create a default button and bind Return to invoke it Source: https://tkdocs.com/tutorial/widgets.html Creates a default button and binds the Return key to invoke it, so pressing Enter triggers the button's command callback. The default option only marks the button; you must add the binding yourself. ```python action = ttk.Button(root, text="Action", default="active", command=myaction) root.bind('', lambda e: action.invoke()) ``` ```tcl ttk::button .action -text "Action" -default active -command myaction bind . ".action invoke" ``` ```ruby action = Tk::Tile::Button.new(root) {text "Action"; default "active"; command {myaction}} root.bind("Return", proc{action.invoke()}) ``` ```perl $action = $mw->new_ttk__button(-text => "Action", -default => "active", -command => sub {myaction();}); Tkx::bind(".", "", sub{$action->invoke();}); ``` -------------------------------- ### Get text widget contents with get method Source: https://tkdocs.com/tutorial/morewidgets.html Retrieves the entire contents of the text widget by calling the get method with the index of the first character (1.0) and the end of the text (end). ```perl $contents = $txt->get("1.0", "end"); ``` -------------------------------- ### Configure a style with Style.configure() Source: https://tkdocs.com/shipman/ttk-style-layer.html Shows how to change the appearance of a style using the configure() method of a ttk.Style instance. ```APIDOC ## Style.configure() ### Description Changes the options of a style. The first argument is the style name, followed by keyword arguments for the options to change. ### Method configure(style_name, **options) ### Parameters - **style_name** (string) - Required - The name of the style to configure. - **options** (keyword arguments) - Optional - Option names and values to set. ### Example ```python s = ttk.Style() s.configure('TButton', foreground='green') ``` ``` -------------------------------- ### Get text widget contents with get method Source: https://tkdocs.com/tutorial/morewidgets.html Retrieves the entire contents of the text widget by calling the get method with the index of the first character (1.0) and the end of the text (end). ```ruby contents = txt.get("1.0", 'end') ``` -------------------------------- ### Initialize listbox and start Tkx main loop Source: https://tkdocs.com/tutorial/morewidgets.html Initializes the listbox interface: colors alternating rows, sets default gift and message variables, selects the first country, and explicitly calls showPopulation because the <> event only fires on user changes. Ends with Tkx::MainLoop(). ```Perl/Tkx for ($i=0; $i<=$#countrynames; $i+=2) { $lbox->itemconfigure($i, -background => "#f0f0ff"); } # Set the starting state of the interface, including selecting the # default gift to send, and clearing the messages. Select the first # country in the list; because the <> event is only # fired when users makes a change, we explicitly call showPopulation. $gift = 'card'; $sentmsg = ""; $statusmsg = ""; $lbox->selection_set(0); showPopulation; Tkx::MainLoop(); ``` -------------------------------- ### Get text widget contents with get command Source: https://tkdocs.com/tutorial/morewidgets.html Retrieves the entire contents of the text widget by calling the get command with the index of the first character (1.0) and the end of the text (end). ```tcl set contents [.txt get 1.0 end] ``` -------------------------------- ### Creating widgets with Tkx::ttk__* commands Source: https://tkdocs.com/tutorial/concepts.html Shows how to create Tk widgets using Tkx::ttk__* functions, passing the widget pathname as the first argument. The example creates a button, a frame, and an entry widget. ```perl Tkx::ttk__button(".b", -text => "hello"); Tkx::ttk__frame(".f"); Tkx::ttk__entry(".f.entry"); ```