Skip to content

Commit 87332b2

Browse files
committed
popup
1 parent 3f0392b commit 87332b2

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

tkinter/label-as-button/main.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
# --- constants ---
6+
7+
FONT_SIZE = 14 # Change this to change all font sizes in the program
8+
9+
# --- classes ---
10+
11+
class MyButton(tk.Label):
12+
13+
def __init__(self, *args, **kwargs):
14+
tk.Label.__init__(self, *args, **kwargs)
15+
16+
# mouse hover
17+
self.bind("<Enter>", self.on_enter)
18+
self.bind("<Leave>", self.on_leave)
19+
20+
self.config(
21+
anchor=tk.CENTER,
22+
background="black",
23+
foreground="white",
24+
font=("default", FONT_SIZE),
25+
)
26+
27+
def on_enter(self, event):
28+
self.config(background="gray")
29+
30+
def on_leave(self, event):
31+
self.config(background="black")
32+
33+
# --- functions ---
34+
35+
def command_exit():
36+
print("exit")
37+
root.destroy()
38+
39+
# --- main ---
40+
41+
root = tk.Tk()
42+
43+
root.geometry("800x600")
44+
root.config(background="#444")
45+
46+
# MyButton
47+
48+
new_game_button = MyButton(root, text="New Game", width=10)
49+
new_game_button.pack(ipadx=10, ipady=10)
50+
51+
load_game_button = MyButton(root, text="Load Game", width=10)
52+
load_game_button.pack(ipadx=10, ipady=10)
53+
54+
options_button = MyButton(root, text="Options", width=10)
55+
options_button.pack(ipadx=10, ipady=10)
56+
57+
# Exit Button
58+
59+
exit_button = tk.Button(root, text="Exit", width=10)
60+
exit_button.pack(ipadx=0, ipady=7)
61+
62+
exit_button.config(
63+
anchor=tk.CENTER,
64+
font=("default", FONT_SIZE),
65+
66+
# without border
67+
borderwidth=0,
68+
highlightthickness=0,
69+
70+
# Leave colors
71+
background="black",
72+
foreground="white",
73+
74+
# Enter colors
75+
activebackground="grey",
76+
activeforeground="white",
77+
78+
# run on click
79+
command=command_exit
80+
)
81+
82+
root.mainloop()

tkinter/popup-menu-class/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
# --- classes ---
6+
7+
class NodePopup(tk.Menu):
8+
9+
def __init__(self, master):
10+
super().__init__(master, tearoff=0)
11+
12+
self.send_disabled = tk.BooleanVar()
13+
14+
self.add_checkbutton(label="Disable sending",
15+
variable=self.send_disabled, command=self.toggle_send)
16+
17+
def popup(self, event):
18+
#self.send_disabled.set(0)
19+
print('send_disabled:', self.send_disabled.get())
20+
self.post(event.x_root, event.y_root)
21+
22+
def toggle_send(self):
23+
print('send_disabled:', self.send_disabled.get())
24+
25+
# --- functions ---
26+
27+
def change():
28+
state = not menu.send_disabled.get()
29+
menu.send_disabled.set(state)
30+
31+
# --- main ---
32+
33+
root = tk.Tk()
34+
root.pack_propagate(0)
35+
36+
menu = NodePopup(root)
37+
root.bind('<Button-3>', menu.popup)
38+
39+
b = tk.Button(root, text='Change', command=change)
40+
b.pack()
41+
42+
root.mainloop()

0 commit comments

Comments
 (0)