tkinter
Clipboard
# auslesen
clipboard = root.clipboard_get()
# neu beschreiben
root.clipboard_clear() # Clipboard löschen
root.clipboard_append(new_text) # Text in die Zwischenablage kopieren
root.update() # Clipboard aktualisieren (optional?)
Infos GUI-Programmierung
root
setup
import tkinter as tk
root = tk.Tk()
# root.geometry("600x400")
# root.resizable(False, False)
root.title("title of window")
# root.columnconfigure((0), weight=1)
# root.rowconfigure((0), weight=1)
# root.configure(background='black')
# root.after(ms, do_something) # run function with delay
# root.state('zoomed') # maximized
# root.attributes('-fullscreen', True) # full-screen (without frame)
# root.attributes("-topmost", 1) # Fenster immer zu oberst
def on_closing():
if tk.messagebox.askokcancel("Beenden", "Möchten Sie die Anwendung wirklich beenden?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing) # Registrieren der Funktion on_closing für das Schließen des Fensters
root.mainloop()
#root.destroy()
grid
Parameters | Meaning |
---|---|
column | The column index where you want to place the widget. |
row | The row index where you want to place the widget. |
rowspan | Set the number of adjacent rows that the widget can span. |
columnspan | Set the number of adjacent columns that the widget can span. |
sticky | If the cell is large than the widget, the sticky option specifies which side the widget should stick to and how to distribute any extra space within the cell that is not taken up by the widget at its original size. |
padx | Add external padding above and below the widget. |
pady | Add external padding to the left and right of the widget. |
ipadx | Add internal padding inside the widget from the left and right sides. |
ipady | Add internal padding inside the widget from the top and bottom sides. |
# .grid_remove() versteckt das Widget, mit .grid() wird es wieder angezeigt
def toggle_visibily():
if widget.winfo_viewable():
widget.grid_remove()
else:
widget.grid()`
bind (detailed)
def quit(event):
caller = event.widget # find out which widget has called the function
root.destroy()
root.bind("<Escape>", quit)
text | event |
---|---|
<Button-1> | left mouse button |
<Button-2> | middle mouse button |
<Button-3> | right mouse button |
<Double-Button-3> | double click right |
<Control-Button-3> | CTRL + right button |
<ButtonRelease> | when released |
<F1> | F1 key |
<f> | "f" key |
<Alt-f> | Alt + "f" |
<Control-f> | CTRL + "f" |
<Shift-f> | Shift + "f" = "F" |
<Down> | cursor down |
<Left> | cursor left |
<Right> | cursor right |
<Up> | cursor up |
<Next> | page down key |
<Prior> | page up key |
<Escape> | according key |
<Tab> | according key |
<BackSpace> | according key |
<Insert> | according key |
<Delete> | according key |
<Home> | according key |
<End> | according key |
<Print> | according key |
<Pause> | according key |
<Return> | according key |
<Control-a> | a (character) |
<Control-Key-1> | 1 (number) |
<KeyPress> | pressing key |
<KeyRelease> | releasing key |
Button (detailed)
setup
button = tk.Button(root, text="show text", width=10, command=do_something)
button.grid(row=0, column=0, sticky="W", padx=10, pady=10)
write
button["text"] = new_text
read
current_text = button["text"] # to be checked
options
Buttons können Bitmaps enthalten
Canvas
< not yet used >
Checkbutton
setup
check_var = tk.StringVar()
checkbutton = tk.Checkbutton(root, text="Press to start", variable=check_var)
checkbutton.grid(row=0, column=0, sticky="W", padx=10, pady=10)
write
check_var.set("Test")
checkbutton["text"] = new_label
read
var_content = check_var.get()
checkbutton["text"] = new_label
options
onvalue = offvalue =
Dialogs
import tkinter.filedialog
file_name = tkinter.filedialog.askopenfilename(title="load")
file_name = tkinter.filedialog.asksaveasfilename(title="save as")
Entry (detailed)(tutorialspoint)(pytutorial)
setup
entry = tk.Entry(root, width=30)
entry.grid(row=0, column=0)
write
entry.delete(0, tk.END) # delete entry
entry.insert(0, text) # add text
read
entry_widget.get() # read entry
options
entry.focus() # set focus to entry
entry["show"] = "*" # hide character, e.g. for passwords
Frame (detailed)
setup
frame = tk.Frame(root)
frame.grid(row=0, column=0, sticky="NSEW")
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
Label (detailed)
setup
label = tk.Label(root, text="some nice text")
label.grid(row=0, column=0, sticky="W", padx=10, pady=10)
write
label["text"] = new_text
read
old_text = label["text"]
options
Label can show bitmaps
LabelFrame (detailed)
setup
labelframe = tk.LabelFrame(root, text="some text")
labelframe.grid(row=0, column=0, sticky="NSEW", padx=10, pady=10)
Listbox (detailed)(tutorialspoint)(pytutorial)
listbox["exportselection"] = False # show selection even if widget is no longer active
listbox["height"] = 5 # number of shown items (!)
listbox["selectmode"] = "extended" # multiple selection possible
listbox.insert("end", "Eintrag")
listbox.delete(0, "end") # clear listbox, 1. param = int != str
clicked_pos = listbox.curselection() # can be a list in case of multi-select
selected_text = listbox.get(clicked_pos)
Scrollbar can be added similar to tk.Text
Messagebox
from tkinter import messagebox
messagebox.askokcancel("Delete all Songs", "Are you sure you want to delete all Songs?")
messagebox.showinfo("Info", "All Songs Deleted")
Menü
# Menüleiste erstellen
menubar = tk.Menu(root)
root.config(menu=menubar)
def exit_app():
root.quit()
# Datei-Menü erstellen
file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Datei", menu=file_menu)
file_menu.add_command(label="Hallo", command=hello)
file_menu.add_separator()
file_menu.add_command(label="Beenden", command=exit_app)
Menubutton
< not yet used > Tutorialspoint
Message
< not yet used > Tutorialspoint
MessageBox
small window showing infos, warnings and so on
Radiobutton (detailed)
var = tk.StringVar()
var.set("any_n")
radiobutton = tk.Radiobutton(root, text="any", variable=var, value="any_n", command=do_something)
radiobutton.grid(row=2, column=0
value_radiobutton = var.get() # str
Scale (tutorialspoint)
= Schieberegler als Eingabe für Zahlen
Scrolledtext
Spinbox (detailed)(tutorialspoint)
Text & Scrollbar (detailed)
text = tk.Text(root, height=15, width=15)
text.grid(row=0, column=0, sticky="EWNS", padx=10, pady=10)
text["state"] = "normal" # or "disabled"
text.config(spacing1=10) # Spacing above the first line in a block of text
text.config(spacing2=10) # Spacing between the lines in a block of text
text.config(spacing3=10) # Spacing after the last line in a block of text
text = tk.Text(root, wrap="none") # kein Zeilenumbruch
text_scroll_y = tk.Scrollbar(root, orient="vertical", command=text.yview)
text_scroll_y.grid(row=0, column=1, sticky="ns", pady=10, padx=(0,10))
text['yscrollcommand'] = text_scroll_y.set # text informs scrollbar about position
text_scroll_x = tk.Scrollbar(root, orient="horizontal", command=text.xview)
text_scroll_x.grid(row=2, column=0, sticky="EW")
text['xscrollcommand'] = text_scroll_x.set # text informs scrollbar about position
text.insert("1.0", "hello") # insert BEFORE exisitng text
text.insert("end", "world") # insert AFTER existing text
text.insert(tk.INSERT, "Hello")
text_content = text.get("1.0", "end") # get text from line 1 char 0 up to the very end
text_content = text.get("1.0", "end-1c") # skip last character e.g. "\n"
text.delete("1.0", "end") # delete content of text widget
text.config(state=tk.DISABLED) # text can't be modified any more
text.config(state=tk.NORMAL)
text.update() # when filling takes some time
text.see(tk.END) # automatically show last line
Toplevel
PanedWindow
Treeview
OOP
Properties
class MyClass:
@property # getter: a = MyClass.value
def value(self):
return self._value
@value.setter # setter: MyClass.value = b
def value(self, new_value):
self._value = new_value
def __setitem__(self, key, value): # MyClass[key] = value
pass