-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI_FallDown.py
123 lines (103 loc) · 4.21 KB
/
GUI_FallDown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'''
May 2017
@author: Burkhard
'''
#======================
# imports
#======================
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
#-----------------------------------------------
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0
def showtip(self, text):
"Display text in a ToolTip window"
self.text = text
if self.tipwindow or not self.text: return
try:
x, y, _cx, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 25
y = y + cy + self.widget.winfo_rooty() +25
self.tipwindow = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
label = tk.Label(tw, text=self.text, justify=tk.LEFT,
background="#ffffe0", relief=tk.SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)
except: pass
def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()
#===================================================================
def createToolTip(widget, text):
toolTip = ToolTip(widget)
def enter(event): toolTip.showtip(text)
def leave(event): toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
#======================
# Create instance
#======================
win = tk.Tk()
#======================
# Add a title
#======================
win.title("Python GUI")
#=========================
# Disable resizing the GUI
#=========================
win.resizable(0,0)
#===============================================================================
# Adding a LabelFrame, Textbox (Entry) and Combobox
#===============================================================================
lFrame = ttk.LabelFrame(win, text="Python GUI Programming Cookbook")
lFrame.grid(column=0, row=0, sticky='WE', padx=10, pady=10)
#===============================================================================
# Labels
#===============================================================================
ttk.Label(lFrame, text="Enter a name:").grid(column=0, row=0)
ttk.Label(lFrame, text="Choose a number:").grid(column=1, row=0, sticky=tk.W)
#===============================================================================
# Buttons click command
#===============================================================================
def clickMe(name, number):
messagebox.showinfo('Information Message Box', 'Hello ' + name + \
', your number is: ' + number)
#===============================================================================
# Creating several controls in a loop
#===============================================================================
names = ['name0', 'name1', 'name2']
nameEntries = ['nameEntry0', 'nameEntry1', 'nameEntry2']
numbers = ['number0', 'number1', 'number2']
numberEntries = ['numberEntry0', 'numberEntry1', 'numberEntry2']
buttons = []
for idx in range(3):
names[idx] = tk.StringVar()
nameEntries[idx] = ttk.Entry(lFrame, width=12, textvariable=names[idx])
nameEntries[idx].grid(column=0, row=idx+1)
nameEntries[idx].delete(0, tk.END)
nameEntries[idx].insert(0, '<name>')
numbers[idx] = tk.StringVar()
numberEntries[idx] = ttk.Combobox(lFrame, width=14, textvariable=numbers[idx])
numberEntries[idx]['values'] = (1+idx, 2+idx, 4+idx, 42+idx, 100+idx)
numberEntries[idx].grid(column=1, row=idx+1)
numberEntries[idx].current(0)
button = ttk.Button(lFrame, text="Click Me "+str(idx+1), command=lambda idx=idx: clickMe(names[idx].get(), numbers[idx].get()))
button.grid(column=2, row=idx+1, sticky=tk.W)
buttons.append(button)
# Add Tooltips to more widgets
createToolTip(nameEntries[idx], 'This is an Entry widget.')
createToolTip(numberEntries[idx], 'This is a DropDown widget.')
createToolTip(buttons[idx], 'This is a Button widget.')
#======================
# Start GUI
#======================
win.mainloop()