Skip to content

Commit 53d0618

Browse files
committed
tkinter
1 parent 585ac63 commit 53d0618

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import tkinter as tk
2+
3+
# --- class ---
4+
5+
class FormatLabel(tk.Label):
6+
7+
def __init__(self, master=None, cnf={}, **kw):
8+
9+
# default values
10+
self._format = '{}'
11+
self._textvariable = None
12+
13+
# get `format` and remove from `kw` so later `Label.__init__` doesn't get it
14+
if 'format' in kw:
15+
self._format = kw['format']
16+
del kw['format']
17+
18+
# get `textvariable` and remove from `kw` so later `Label.__init__` doesn't get it
19+
# Assign own function to format text in Label when variable change value
20+
if 'textvariable' in kw:
21+
self._textvariable = kw['textvariable']
22+
self._textvariable.trace('w', self._update_text)
23+
del kw['textvariable']
24+
25+
# run `Label.__init__` without `format` and `textvariable`
26+
super().__init__(master, cnf={}, **kw)
27+
28+
# update text after running `Label.__init__`
29+
if self._textvariable:
30+
self._update_text(self._textvariable, '', 'w')
31+
32+
def _update_text(self, a, b, c):
33+
"""update text in label when variable change value"""
34+
#print(f'|{a}|{b}|{c}|')
35+
self["text"] = self._format.format(self._textvariable.get())
36+
37+
# --- main ---
38+
39+
root = tk.Tk()
40+
41+
myvar = tk.DoubleVar(value=0.05)
42+
43+
label = FormatLabel(root, textvariable=myvar, format="Today: {:.0%} and growing")
44+
#label = FormatLabel(root, textvariable=myvar, format="{:.0%}")
45+
label.pack()
46+
47+
myvar.set(0.1)
48+
49+
root.mainloop()

tkinter/splash-screen/main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2019.12.11
4+
5+
import tkinter as tk
6+
7+
WIDTH = 800
8+
HEIGHT = 600
9+
root = tk.Tk()
10+
11+
12+
screen_width = root.winfo_screenwidth()
13+
screen_height = root.winfo_screenheight()
14+
x = (screen_width-WIDTH)//2
15+
y = (screen_height-HEIGHT)//2
16+
root.geometry('{}x{}+{}+{}'.format(WIDTH, HEIGHT, x, y))
17+
18+
root.overrideredirect(True) # borderless window, can't be close and it is always on top
19+
#root.update_idletasks() # DON'T need it but if it is needed then can't be before `overrideredirect()`
20+
21+
button = tk.Button(root, text='QUIT', command=root.destroy)
22+
button.place(relx=.5, rely=0.5, anchor='center')
23+
24+
root.mainloop()
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2019.12.14
4+
#
5+
6+
import tkinter as tk
7+
import tkinter.scrolledtext as ScrolledText
8+
9+
root = tk.Tk()
10+
#text_widget = tk.Text()
11+
text_widget = ScrolledText.ScrolledText()
12+
text_widget.pack(fill='both', expand=True)
13+
14+
label1 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#d0ffff', justify='left', padx=10, pady=5)
15+
label2 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#ffffd0', justify='left', padx=10, pady=5)
16+
label3 = tk.Label(text_widget, text="Hello\nWorld!!!", background='#d0ffff', justify='left', padx=10, pady=5)
17+
18+
text_widget.tag_configure('tag-left', justify='left')
19+
text_widget.tag_configure('tag-right', justify='right')
20+
21+
22+
text_widget.insert('end', '\n')
23+
text_widget.window_create('end', window=label1)
24+
25+
text_widget.insert('end', '\n ', 'tag-right') # space to move right Label
26+
text_widget.window_create('end', window=label2)
27+
28+
text_widget.insert('end', '\n')
29+
text_widget.window_create('end', window=label3)
30+
31+
root.mainloop()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2019.12.14
4+
#
5+
6+
import tkinter as tk
7+
import tkinter.scrolledtext as ScrolledText
8+
9+
root = tk.Tk()
10+
#text_widget = tk.Text()
11+
text_widget = ScrolledText.ScrolledText()
12+
text_widget.pack(fill='both', expand=True)
13+
14+
text_widget.tag_configure('tag-center', justify='center')
15+
text_widget.tag_configure('tag-left', justify='left')
16+
text_widget.tag_configure('tag-right', justify='right', background='red')
17+
18+
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-center')
19+
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-left')
20+
text_widget.insert('end', 'Hello\nWorld!!!\n', 'tag-right')
21+
22+
root.mainloop()

0 commit comments

Comments
 (0)