Expand description
High-level bindings to Tk 8.6
The crate tk is bindings to Tk commands, aiming at:
-
Make Tk programers feel at home if possible.
-
Provide for non-Tk-programers easy-to-use API to start writing Tk GUI programs under constraints of Rust types, without the need of concatenating command strings of too flexible arguments.
A quick glance
use tk::*;
use tk::cmd::*;
fn main() -> TkResult<()> {
let tk = make_tk!()?;
let root = tk.root();
root.add_label( -text("constructs widgets and layout step by step") )?
.pack(())?;
let f = root
.add_frame(())?
.pack(())?;
let _btn = f
.add_button( "btn" -text("quit") -command("destroy .") )?
.pack(())?;
Ok( main_loop() )
}
Another glance
use tk::*;
use tk::cmd::*;
fn main() -> TkResult<()> {
let tk = make_tk!()?;
tk.root().add_widgets(
-pack( -label( -text("constructs widgets and layout in one expression") ))
-pack( -frame( -pack( -button( "btn" -text("quit") -command("destroy .") ))))
)?;
Ok( main_loop() )
}
The naming conventions in translating Tk commands to Rust bindings
-
Prefix Tk widget constructors with
add_
and put parentheses around option values.The Tk command to add a widget looks like
Constructor path -options_and_values
, e.g.label .lb -text "lorem ipsum" -width 50 -height 20
The equivalent Rust statement is as follows.
let lb = root.add_label( /*".lb"*/ -text("lorem ipsum") -width(50) -height(20) )?;
-
Converts Tcl’s imperative style to Rust’s object style
The Tk command is in the form of “verb noun options”, e.g.
pack .lb -fill both
The equivalent Rust statement is in th form of “object method options”, as follows.
lb.pack( -fill("both") )?; // use pack(()) without any option.
-
Converts Tk’s space-separated commands to Rust’s underscore-separated function names.
Tk commands are space-separated, e.g.
tk fontchooser show
The equivalent Rust statement is as follows.
tk.fontchooser_show()?;
Users can look into the Tk command reference and find the “fontchooser” page then search “show”.
-
Distinguish between set and get via the
set_
prefix.In Tk, it is common to distinguish set and get by providing or omitting the value argument, e.g.
wm title window "Lorem ipsum"
means to set the window’s title to “Lorem ipsum”, whilewm title window
means to get the windows’ title.The equivalent Rust statements are as follows.
window.set_wm_title( "Lorem ipsum" )?; window.wm_title()?;
Documents
Re-exports
pub use bitmap::Bitmap;
pub use error::TkError;
pub use error::TkResult;
pub use cmd::TkRoot;
pub use cmd::Widget;
pub use cmd::no_arg;
pub use cmd::path_seg;
pub use key_syms::TkKey;
pub use photo::Photo;
pub use query::UpcastFrom;
pub use query::UpcastableWidget;
pub use query::CreatedWidgets;
pub use ttk_widget::TtkCommonTraits;
pub use ttk_widget::TtkState;
pub use ttk_widget::TtkStateSpec;
pub use event::TkEventSeq;
pub use image::Image;
pub use wm::TkFocusModel;
pub use wm::WmManage;
pub use range::TkDefaultStart;
pub use range::TkDefaultEnd;
pub use traits::Delete;
pub use traits::TkBBoxTrait;
pub use traits::TkEntryTraits;
pub use traits::TkXView;
pub use traits::TkXViewIndex;
pub use traits::TkYView;
pub use traits::TkYViewIndex;
pub use types::TkBBox;
pub use types::TkColor;
pub use types::TkCoord;
pub use types::TkHandler;
pub use types::TkGeometry;
pub use types::TkDistance;
pub use types::TkDLine;
pub use types::TkRGB;
pub use types::TkRectangle;
pub use types::TkResizable;
pub use types::TkRequester;
pub use types::TkState;
pub use types::TkSize;
pub use types::TkScreenName;
pub use types::TkVisualClass;
pub use types::TkWindowingSystem;
pub use types::TtkInsertPos;
pub use types::TtkTreeviewRegion;
pub use button::TkButton;
pub use canvas::TkCanvas;
pub use checkbutton::TkCheckbutton;
pub use entry::TkEntry;
pub use entry::Index as TkEntryIndex;
pub use frame::TkFrame;
pub use label::TkLabel;
pub use labelframe::TkLabelframe;
pub use listbox::Index as TkListboxIndex;
pub use listbox::TkListbox;
pub use menu::AddMenus;
pub use menu::Index as TkMenuIndex;
pub use menu::TkMenu;
pub use menu::TkMenuCloneType;
pub use menu::TkMenuEntryType;
pub use menubutton::TkMenubutton;
pub use message::TkMessage;
pub use panedwindow::TkPanedwindow;
pub use panedwindow::TkSashOrHandle;
pub use radiobutton::TkRadiobutton;
pub use scale::TkScale;
pub use scale::TkScaleCoord;
pub use scale::TkScalePart;
pub use scrollbar::TkScrollbar;
pub use scrollbar::TkScrollbarElement;
pub use scrollbar::TkScrollbarDelta;
pub use spinbox::TkSpinbox;
pub use spinbox::TkSpinboxElement;
pub use spinbox::TkSpinboxInvokableElement;
pub use text::TkCmp;
pub use text::TkDump;
pub use text::TkText;
pub use text::TkTextMarkGravity;
pub use text::TkTextSearch;
pub use text::TkTextSearchAll;
pub use toplevel::TkToplevel;
pub use ttk_button::TtkButton;
pub use ttk_checkbutton::TtkCheckbutton;
pub use ttk_combobox::TtkCombobox;
pub use ttk_combobox::Index as TtkComboboxIndex;
pub use ttk_entry::Index as TtkEntryIndex;
pub use ttk_entry::TtkEntry;
pub use ttk_frame::TtkFrame;
pub use ttk_label::TtkLabel;
pub use ttk_labelframe::TtkLabelframe;
pub use ttk_menubutton::TtkMenubutton;
pub use ttk_notebook::TtkNotebook;
pub use ttk_notebook::TtkNotebookTabId;
pub use ttk_panedwindow::TtkPanedwindow;
pub use ttk_progressbar::TtkProgressbar;
pub use ttk_progressbar::TtkProgressbarInterval;
pub use ttk_radiobutton::TtkRadiobutton;
pub use ttk_scale::TtkScale;
pub use ttk_scrollbar::TtkScrollbar;
pub use ttk_separator::TtkSeparator;
pub use ttk_sizegrip::TtkSizegrip;
pub use ttk_spinbox::TtkSpinbox;
pub use ttk_treeview::Index as TtkTreeviewIndex;
pub use ttk_treeview::TtkTreeview;
pub use ttk_treeview::Column as TtkTreeviewColumn;
pub use font::Font;
pub use ext::AddHBox;
pub use ext::AddVBox;
pub use ext::HBox;
pub use ext::HBoxResize;
pub use ext::VBox;
pub use ext::VBoxResize;
Modules
Macros
Structs
- Tk instance.
Functions
- Loop for events until all windows are deleted.
Type Aliases
- Error from Tcl interpreter.