show line numbers in conde snippets

This commit is contained in:
Marcello 2023-10-20 18:22:46 +02:00
parent cd1df0e376
commit 255a68d673
82 changed files with 1249 additions and 1251 deletions

View file

@ -2,7 +2,7 @@
## Standard Imports
```py
```py linenums="1"
from tkinter import * # import Python Tk Binding
from tkinter import ttk # import Themed Widgets
```
@ -19,7 +19,7 @@ geometry managers determine size and oder widget drawing properties
event loop receives events from the OS
customizable events provide a callback as a widget configuration
```py
```py linenums="1"
widget.bind('event', function) # method to capture any event and than execute an arbitrary piece of code (generally a function or lambda)
```
@ -29,7 +29,7 @@ VIRTUAL EVENT --> hig level event generated by widget (listed in widget docs)
Widgets are objects and all things on screen. All widgets are children of a window.
```py
```py linenums="1"
widget_name = tk_object(parent_window) # widget is inserted into widget hierarchy
```
@ -37,7 +37,7 @@ widget_name = tk_object(parent_window) # widget is inserted into widget hierarc
Displays a single rectangle, used as container for other widgets
```py
```py linenums="1"
frame = ttk.Frame(parent, width=None, height=None, borderwidth=num:int)
# BORDERWIDTH: sets frame border width (default: 0)
# width, height MUST be specified if frame is empty, otherwise determined by parent geometry manager
@ -47,7 +47,7 @@ frame = ttk.Frame(parent, width=None, height=None, borderwidth=num:int)
Extra space inside widget (margin).
```py
```py linenums="1"
frame['padding'] = num # same padding for every border
frame['padding'] = (horizontal, vertical) # set horizontal THEN vertical padding
frame['padding'] = (left, top, right, bottom) # set left, top, right, bottom padding
@ -60,13 +60,13 @@ frame['relief'] = border_style
Display text or image without interactivity.
```py
```py linenums="1"
label = ttk.Label(parent, text='label text')
```
### DEFINING UPDATING LABEL
```py
```py linenums="1"
var = StringVar() # variable containing text, watches for changes. Use get, set methods to interact with the value
label['textvariable'] = var # attach var to label (only of type StringVar)
var.set("new text label") # change label text
@ -74,14 +74,14 @@ var.set("new text label") # change label text
### DISPLAY IMAGES (2 steps)
```py
```py linenums="1"
image = PhotoImage(file='filename') # create image object
label['image'] = image # use image config
```
### DISPLAY IMAGE AND-OR TEXT
```py
```py linenums="1"
label['compound'] = value
```
@ -97,20 +97,20 @@ Compound value:
Specifies edge or corner that the label is attached.
```py
```py linenums="1"
label['anchor'] = compass_direction #compass_direction: n, ne, e, se, s, sw, w, nw, center
```
### MULTI-LINE TEXT WRAP
```py
```py linenums="1"
# use \n for multi line text
label['wraplength'] = size # max line length
```
### CONTROL TEXT JUSTIFICATION
```py
```py linenums="1"
label['justify'] = value #value: left, center, right
label['relief'] = label_style
@ -120,7 +120,7 @@ label['background'] = color # color passed with name or HEX RGB codes
### FONT STYLE (use with caution)
```py
```py linenums="1"
# used outside style option
label['font'] = font
```
@ -141,19 +141,19 @@ Fonts:
Press to perform some action
```py
```py linenums="1"
button = ttk.Button(parent, text='button_text', command=action_performed)
```
### TEXT or IMAGE
```py
```py linenums="1"
button['text/textvariable'], button['image'], button['compound']
```
### BUTTON INVOCATION
```py
```py linenums="1"
button.invoke() # button activation in the program
```
@ -161,7 +161,7 @@ button.invoke() # button activation in the program
Activate or deactivate the widget.
```py
```py linenums="1"
button.state(['disabled']) # set the disabled flag, disabling the button
button.state(['!disabled']) # clear the disabled flag
button.instate(['disabled']) # return true if the button is disabled, else false
@ -174,7 +174,7 @@ button.instate(['!disabled'], cmd) # execute 'cmd' if the button is not disable
Button with binary value of some kind (e.g a toggle) and also invokes a command callback
```py
```py linenums="1"
checkbutton_var = TkVarType
check = ttk.Checkbutton(parent, text='button text', command=action_performed, variable=checkbutton_var, onvalue=value_on, offvalue=value_off)
```
@ -189,7 +189,7 @@ checkbutton won't set the linked variable (MUST be done in the program)
### CONFIG OPTIONS
```py
```py linenums="1"
check['text/textvariable']
check['image']
check['compound']
@ -201,7 +201,7 @@ check.instate(['flag'])
Multiple-choice selection (good if options are few).
```py
```py linenums="1"
#RADIOBUTTON CREATION (usually as a set)
radio_var = TkVarType
radio_1 = ttk.Radiobutton(parent, text='button text', variable=radio_var, value=button_1_value)
@ -221,7 +221,7 @@ radio.instate(['flag'])
Single line text field accepting a string.
```py
```py linenums="1"
entry_var = StringVar()
entry = ttk.Entry(parent, textvariable=entry_var, width=char_num, show=symbol)
# SHOW: replaces the entry test with symbol, used for password
@ -230,7 +230,7 @@ entry = ttk.Entry(parent, textvariable=entry_var, width=char_num, show=symbol)
### CHANGE ENTRY VALUE
```py
```py linenums="1"
entry.get() # returns entry value
entry.delete(start, 'end') # delete between two indices, 0-based
entry.insert(index, 'text value') # insert new text at a given index
@ -238,7 +238,7 @@ entry.insert(index, 'text value') # insert new text at a given index
### ENTRY CONFIG OPTIONS
```py
```py linenums="1"
radio.state(['flag'])
radio.instate(['flag'])
```
@ -247,7 +247,7 @@ radio.instate(['flag'])
Drop-down list of available options.
```py
```py linenums="1"
combobox_var = StringVar()
combo = ttk.Combobox(parent, textvariable=combobox_var)
combobox.get() # return combobox current value
@ -259,7 +259,7 @@ combobox.bind('<<ComboboxSelected>>', function)
### PREDEFINED VALUES
```py
```py linenums="1"
combobox['values'] = (value_1, value_2, ...) # provides a list of choose-able values
combobox.state(['readonly']) # restricts choose-able values to those provided with 'values' config option
# SUGGESTION: call selection clear method on value change (on ComboboxSelected event) to avoid visual oddities
@ -269,7 +269,7 @@ combobox.state(['readonly']) # restricts choose-able values to those provided w
Display list of single-line items, allows browsing and multiple selection (part og Tk classic, missing in themed Tk widgets).
```py
```py linenums="1"
lstbx = Listbox(parent, height=num, listvariable=item_list:list)
# listvariable links a variable (MUST BE a list) to the listbox, each element is a item of the listbox
# manipulation of the list changes the listbox
@ -277,7 +277,7 @@ lstbx = Listbox(parent, height=num, listvariable=item_list:list)
### SELECTING ITEMS
```py
```py linenums="1"
lstbx['selectmode'] = mode # MODE: browse (single selection), extended (multiple selection)
lstbx.curselection() # returns list of indices of selected items
# on selection change: generate event <ListboxSelect>
@ -288,7 +288,7 @@ lstbx.curselection() # returns list of indices of selected items
## SCROLLBAR
```py
```py linenums="1"
scroll = ttk.Scrollbar(parent, orient=direction, command=widget.view)
# ORIENT: VERTICAL, HORIZONTAL
# WIDGET.VIEW: .xview, .yview
@ -301,7 +301,7 @@ widget.configure(yscrollcommand=scroll.set)
Box in right bottom of widget, allows resize.
```py
```py linenums="1"
ttk.Sizegrip(parent).grid(column=999, row=999, sticky=(S, E))
```
@ -309,7 +309,7 @@ ttk.Sizegrip(parent).grid(column=999, row=999, sticky=(S, E))
Area accepting multiple line of text.
```py
```py linenums="1"
txt = Text(parent, width=num:int, height=num:int, wrap=flag) # width is character num, height is row num
# FLAG: none (no wrapping), char (wrap at every character), word (wrap at word boundaries)
txt['state'] = flag # FLAG: disabled, normal
@ -323,7 +323,7 @@ txt.delete(start, end) # delete range of text
Feedback about progress of lenghty operation.
```py
```py linenums="1"
progbar = ttk.Progressbar(parent, orient=direction, length=num:int, value=num, maximum=num:float mode=mode)
# DIRECTION: VERTICAL, HORIZONTAL
# MODE: determinate (relative progress of completion), indeterminate (no estimate of completion)
@ -334,13 +334,13 @@ progbar = ttk.Progressbar(parent, orient=direction, length=num:int, value=num, m
### DETERMINATE PROGRESS
```py
```py linenums="1"
progbar.step(amount) # increment value of given amount (DEFAULT: 1.0)
```
### INDETERMINATE PROGRESS
```py
```py linenums="1"
progbar.start() # starts progressbar
progbar.stop() #stoops progressbar
```
@ -349,7 +349,7 @@ progbar.stop() #stoops progressbar
Provide a numeric value through direct manipulation.
```py
```py linenums="1"
scale = ttk.Scale(parent, orient=DIR, length=num:int, from_=num:float, to=num:float, command=cmd)
# COMMAND: calls cmd at every scale change, appends current value to func call
scale['value'] # set or read current value
@ -361,7 +361,7 @@ scale.get() # get current value
Choose numbers. The spinbox choses item from a list, arrows permit cycling lits items.
```py
```py linenums="1"
spinval = StringVar()
spin = Spinbox(parent, from_=num, to=num, textvariable=spinval, increment=num, value=lst, wrap=boolean)
# INCREMENT specifies increment\decrement by arrow button
@ -402,7 +402,7 @@ A single value for the option puts the same padding on both left and right (or t
while a two-value list lets you put different amounts on left and right (or top and bottom).
To add padding around an entire row or column, the "columnconfigure" and "rowconfigure" methods accept a "pad" option.
```py
```py linenums="1"
widget.grid(column=num, row=num, columnspan=num, rowspan=num, sticky=(), padx=num, pady=num) # sticky: N, S, E, W
widget.columnconfigure(pad=num, weight=num)
widget.rowconfigure(pad=num, weight=num)
@ -419,7 +419,7 @@ widget.grid_remove(slaves) # takes a list of slaves, removes slaves from grid (
### CREATING TOPLEVEL WINDOW
```py
```py linenums="1"
tlw = Toplevel(parent) # parent of root window, no need to grid it
window.destroy()
@ -429,7 +429,7 @@ window.destroy()
### CHANGING BEHAVIOR AND STYLE
```py
```py linenums="1"
# WINDOW TILE
window.title() # returns title of the window
window.title('new title') # sets title
@ -469,7 +469,7 @@ window.deiconify() # deiconifies window
### STANDARD DIALOGS
```py
```py linenums="1"
# SLEETING FILE AND DIRECTORIES
# on Windows and Mac invokes underlying OS dialogs directly
from tkinter import filedialog
@ -507,7 +507,7 @@ POSSIBLE ALERT/CONFIRMATION RETURN VALUES:
## SEPARATOR
```py
```py linenums="1"
# horizontal or vertical line between groups of widgets
separator = ttk.Separator(parent, orient=direction)
# DIRECTION: horizontal, vertical
@ -532,7 +532,7 @@ pw.forget(position) # remove widget from pane
Allows switching between multiple pages
```py
```py linenums="1"
nb = ttk.Notebook(parent)
f1 = ttk.Frame(parent, ...) # child of notebook
f2 = ttk.Frame(parent, ...)
@ -555,7 +555,7 @@ nb.tab(tabid, option=value) # change tab option
Creation of personalized fonts
```py
```py linenums="1"
from tkinter import font
font_name = font.Font(family='font_family', size=num, weight='bold/normal', slant='roman/italic', underline=boolean, overstrike=boolean)
# FAMILY: Courier, Times, Helvetica (support guaranteed)
@ -573,7 +573,7 @@ label['image'] = imgobj
#### IMAGES W/ Pillow
```py
```py linenums="1"
from PIL import ImageTk, Image
myimg = ImageTk.PhotoImage(Image.open('filename'))
```