This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
165 lines (135 loc) · 5.42 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
__author__ = 'Charles Engen'
# This import statement imports our custom db manager to use with the GUI
# You always want to keep separate parts from intermingling. This
# prevents bugs.
import database_manager
# This is the GUI manager, it allows us to create all the wonderful
# GUI elements that we need, we just have to program them. I use this
# one because it is built in and I do not want to create OpenGL widgets
# for this type of project.
import tkinter
class ScrolledTextArea:
"""
This class will be instantiated in our main class. The purpose of this class
is to create a text area to display the names of our family database.
"""
def __init__(self, parent):
"""
This method is always called when our class is used, it creates
a tkinter frame object and .packs() it. This means that tells tkinter
to display it on the screen.
It also sets the text field and creates a textpad(our method) area to work with.
:param parent: Pass the parent widget
:return: None
"""
self.frame = tkinter.Frame(parent)
self.frame.pack()
self.text = None
self.textPad(self.frame)
def textPad(self, parent):
"""
This method is a widget framework that is used to create a area
for use to display our text.
:param parent: Pass the parent widget
:return: None
"""
textPad = tkinter.Frame()
self.text = tkinter.Text(textPad, height=10, width=20)
scroll = tkinter.Scrollbar(textPad)
self.text.configure(yscrollcommand=scroll.set)
self.text.pack(side='left')
scroll.pack(side="right", fill='y')
textPad.pack(side="top")
def add_text(self, data):
"""
This method allows us to add text to our widget when needed.
First it clears the text area and then populates it with the
passed in data, which can be any iterable data type.
:param data:
:return:
"""
self.text.delete('1.0', tkinter.END)
for line in data:
self.text.insert(tkinter.INSERT, line[1], tkinter.END, "\n")
class MainFrame(tkinter.Frame):
"""
This is the main frame of our GUI, it is what will contin all the other
widgets and manage trier size and such.
TODO:
Add automatic resizing for children widgets
"""
def __init__(self):
"""
This method is called every time we use our main frame widget.
There are several components to it and I have labeled them
accordingly.
:return:
"""
tkinter.Frame.__init__(self)
# Font Data
self.font_data_display = ('times', 16)
self.font_data_menu = ('times', 14)
# This is the text area where the names will be displayed
self.display_text = tkinter.Label(self, text="")
self.text = ScrolledTextArea(self)
# End text area
# This is the Input area
self.input_var_name = tkinter.StringVar()
self.input_widget = tkinter.Entry(self, textvariable=self.input_var_name,
font=self.font_data_display, relief=tkinter.SUNKEN)
# Here we bind the enter key to the entry widgets submit option
self.input_widget.bind('<Return>', self.submit_name)
self.input_widget.bind('<Button-1>', self.clear_text_area)
# This packs all the widgets so they will get displayed
self.display_text.pack(side="top")
self.pack(side="right")
self.input_widget.pack(side="left")
# This will update the text box
self.update_text_area()
def set_entry_box(self):
"""
This method gives us the ability to set the input box's field before entry.
:return: None
"""
self.input_var_name.set("Please enter the Full Name.")
def update_text_area(self):
"""
This method updates the input area, it also grabs the input and stores it into the
database using the manager
:return: None
"""
self.text.add_text(database_manager.get_family_members())
self.set_entry_box()
def submit_name(self, *args, **kwargs):
"""
This method gives us the ability to submit our entry box's information into our database,
it also updates it after 400 milli-secs.
:param args: Tkinter pass information
:param kwargs: Tkinter pass information
:return:
"""
database_manager.create_family_member(self.input_var_name.get())
self.after(400, self.update_text_area)
def clear_text_area(self, *args, **kwargs):
"""
This method clears the entry widgets text area.
:param args: Tkinter pass information
:param kwargs: Tkinter pass information
:return:
"""
self.input_var_name.set("")
def mainloop_():
"""
This function is our mainloop, it will decide what runs when and how.
:return: None
"""
root = MainFrame()
root.mainloop()
# The following code uses the fact that python calls a module
# __main__ if it is being used as the main file, when that is the case
# it runs the code.
if __name__ == "__main__":
database_manager.create_family_member("John Apple") # we add these entries to populate our database del of needed
database_manager.create_family_member("Ruth Anne")
database_manager.create_family_member("Apple May")
mainloop_() # Runs the mainloop