· 5 years ago · May 11, 2020, 07:40 AM
1from tkinter import *
2from tkinter import ttk
3from tkinter import messagebox
4import sqlite3 as sq
5
6from configparser import ConfigParser
7
8
9
10#---------------------------------------------------------
11# Settings file
12#---------------------------------------------------------
13
14# Check if file already exists
15import os
16
17config_default = ConfigParser()
18#default config file creation, for use if none exists.
19config_default['keyboard_controls'] = {
20 'Throttle_up' : 'w',
21 'Throttle_down' : 's',
22 'Yaw_left' : 'a',
23 'Yaw_right' : 'd',
24 'Pitch_up' : 'up',
25 'Pitch_down' : 'down',
26 'Roll_left' : 'left',
27 'Roll_right' : 'right'
28}
29
30config_default['speed_settings'] = {
31 'Throttle_up' : '10',
32 'Throttle_down' : '10',
33 'Yaw_left' : '10',
34 'Yaw_right' : '10',
35 'Pitch_up' : '10',
36 'Pitch_down' : '10',
37 'Roll_left' : '10',
38 'Roll_right' : '10'
39}
40
41config_default['Menu_shortcuts'] = {
42 'Help' : 'F1',
43 'Settings' : 'F8'
44}
45
46
47#check if there already is a config file, if so use that
48file_config_temp = os.path.dirname(os.path.realpath(__file__)) + os.sep + "config.ini"
49
50if not os.path.exists(file_config_temp) or os.stat(file_config_temp).st_size == 0:
51 with open('config.ini', 'w') as config_file_temp:
52 config_default.write(config_file_temp)
53
54file_config = ConfigParser()
55file_config.read('config.ini')
56
57
58
59#---------------------------------------------------------
60# GUI
61#---------------------------------------------------------
62
63
64def raise_frame(frame):
65 frame.tkraise()
66
67root = Tk()
68
69
70interface = Frame(root,width=1280, height=720)
71settings = Frame(root)
72login = Frame(root)
73
74
75for frame in (interface, settings, login):
76 frame.grid(row=0, column=0, sticky='news')
77
78#---------------------------------------------------------
79# Main Interface
80#---------------------------------------------------------
81
82# settings button
83Button(interface, text='Settings', command=lambda:raise_frame(settings)).place(x=10, y=10)
84
85
86# Connect / disconnect button
87is_connected = False # used to change text on connected / disconnect button
88
89if is_connected == False:
90 is_connected_text = "Disconnected"
91 Button(interface, text=is_connected_text, bg='red',command=lambda: raise_frame(login)).place(x=10, y=50)
92
93else:
94 is_connected_text = "Connected"
95 Button(interface, text=is_connected_text, bg='green').place(x=10, y=50)
96
97
98
99render = PhotoImage(file = "controls.jpg")
100
101
102
103#---------------------------------------------------------
104# Settings
105#---------------------------------------------------------
106
107Label(settings, text='Settings').pack(side=TOP, anchor=CENTER)
108Button(settings, text='Back', command=lambda:raise_frame(interface)).place(x=10, y=10)
109
110settings.grid_columnconfigure((0,1), weight=1)
111
112Label(settings, text="Throttle_up").place(x=20, y=50)
113Throttle_up = Entry(settings, bd =5)
114Throttle_up.insert(0, file_config.get('keyboard_controls', 'Throttle_up'))
115Throttle_up.place(x=100, y=50)
116
117Label(settings, text="Throttle_down").place(x=20, y=80)
118Throttle_down = Entry(settings, bd =5)
119Throttle_down.insert(0, file_config.get('keyboard_controls', 'Throttle_down'))
120Throttle_down.place(x=100, y=80)
121
122Label(settings, text="Yaw_left").place(x=20, y=110)
123Yaw_left = Entry(settings, bd =5)
124Yaw_left.insert(0, file_config.get('keyboard_controls', 'Yaw_left'))
125Yaw_left.place(x=100, y=110)
126
127Label(settings, text="Yaw_right").place(x=20, y=140)
128Yaw_right = Entry(settings, bd =5)
129Yaw_right.insert(0, file_config.get('keyboard_controls', 'Yaw_right'))
130Yaw_right.place(x=100, y=140)
131
132Label(settings, text="Pitch_up").place(x=20, y=170)
133Pitch_up = Entry(settings, bd =5)
134Pitch_up.insert(0, file_config.get('keyboard_controls', 'Pitch_up'))
135Pitch_up.place(x=100, y=170)
136
137Label(settings, text="Pitch_down").place(x=20, y=200)
138Pitch_down = Entry(settings, bd =5)
139Pitch_down.insert(0, file_config.get('keyboard_controls', 'Pitch_down'))
140Pitch_down.place(x=100, y=200)
141
142Label(settings, text="Roll_left").place(x=20, y=230)
143Roll_left = Entry(settings, bd =5)
144Roll_left.insert(0, file_config.get('keyboard_controls', 'Roll_left'))
145Roll_left.place(x=100, y=230)
146
147Label(settings, text="Roll_right").place(x=20, y=260)
148Roll_right = Entry(settings, bd =5)
149Roll_right.insert(0, file_config.get('keyboard_controls', 'Roll_right'))
150Roll_right.place(x=100, y=260)
151#--------------------------------------------------------------------
152
153Label(settings, text="Throttle_up").place(x=520, y=50)
154Throttle_up = Entry(settings, bd =5)
155Throttle_up.insert(0, file_config.get('speed_settings', 'Throttle_up'))
156Throttle_up.place(x=600, y=50)
157
158Label(settings, text="Throttle_down").place(x=520, y=80)
159Throttle_down = Entry(settings, bd =5)
160Throttle_down.insert(0, file_config.get('speed_settings', 'Throttle_down'))
161Throttle_down.place(x=600, y=80)
162
163Label(settings, text="Yaw_left").place(x=520, y=110)
164Yaw_left = Entry(settings, bd =5)
165Yaw_left.insert(0, file_config.get('speed_settings', 'Yaw_left'))
166Yaw_left.place(x=600, y=110)
167
168Label(settings, text="Yaw_right").place(x=520, y=140)
169Yaw_right = Entry(settings, bd =5)
170Yaw_right.insert(0, file_config.get('speed_settings', 'Yaw_right'))
171Yaw_right.place(x=600, y=140)
172
173Label(settings, text="Pitch_up").place(x=520, y=170)
174Pitch_up = Entry(settings, bd =5)
175Pitch_up.insert(0, file_config.get('speed_settings', 'Pitch_up'))
176Pitch_up.place(x=600, y=170)
177
178Label(settings, text="Pitch_down").place(x=520, y=200)
179Pitch_down = Entry(settings, bd =5)
180Pitch_down.insert(0, file_config.get('speed_settings', 'Pitch_down'))
181Pitch_down.place(x=600, y=200)
182
183Label(settings, text="Roll_left").place(x=520, y=230)
184Roll_left = Entry(settings, bd =5)
185Roll_left.insert(0, file_config.get('speed_settings', 'Roll_left'))
186Roll_left.place(x=600, y=230)
187
188Label(settings, text="Roll_right").place(x=520, y=260)
189Roll_right = Entry(settings, bd =5)
190Roll_right.insert(0, file_config.get('speed_settings', 'Roll_right'))
191Roll_right.place(x=600, y=260)
192
193#--------------------------------------------------------------------
194Label(settings, text="Help").place(x=20, y=360)
195Help = Entry(settings, bd =5)
196Help.insert(0, file_config.get('Menu_shortcuts', 'Help'))
197Help.place(x=100, y=360)
198
199Label(settings, text="settings").place(x=20, y=400)
200Settings_text = Entry(settings, bd =5)
201Settings_text.insert(0, file_config.get('Menu_shortcuts', 'Settings'))
202Settings_text.place(x=100, y=400)
203
204#---------------------------------------------------------
205# Login
206#---------------------------------------------------------
207
208Label(login, text='Login').pack(side=TOP, anchor=CENTER)
209Button(login, text='Back', command=lambda:raise_frame(interface)).place(x=10, y=10)
210
211
212conn = sq.connect('IP_address.db') # name of database file
213cur = conn.cursor()
214
215cur.execute('create table if not exists tasks (title text)')
216
217task = []
218
219def listUpdate():
220 clearList()
221 for i in task:
222 list_box.insert('end', i)
223
224def clearList():
225 list_box.delete(0, 'end')
226
227def addIP():
228 address = input_box_ip_address.get()
229 if len(address) == 0:
230 messagebox.showinfo('No IP Address Entered', 'Enter IP Address')
231 else:
232 task.append(address)
233 cur.execute('insert into tasks values (?)', (address,))
234 listUpdate()
235 input_box_ip_address.delete(0, 'end')
236
237
238def delOne():
239 try:
240 selection = list_box.get(list_box.curselection())
241 if selection in task:
242 task.remove(selection)
243 listUpdate()
244 cur.execute('delete from tasks where title = ?', (selection,))
245 except:
246 messagebox.showinfo('Remove', 'No IP Address selected')
247
248
249def retrieveDB():
250 while (len(task) != 0):
251 task.pop()
252 for row in cur.execute('select title from tasks'):
253 task.append(row[0])
254
255def user_login_check():
256 print("add some code, for login password check")
257 # this is for the login check.
258 # add code here or use what is already made to check for correct login and password.
259 # remember to read the IP address from the list_box "IP_current_selected = list_box.get(list_box.curselection())"
260 # Maybe add a popup that says wrong login or password if incorrect.
261 # If no ip address is selected, give a message about that too.
262
263
264# Login stuff
265input_box_password = Entry(login, width=18).place(x=50, y=80)
266input_box_user_name = Entry(login, width=18).place(x=200, y=80)
267add_login_button = Button(login, text='Login', width=10, command=user_login_check).place(x=140, y=120)
268
269
270user_name_text = Label(login, text='Username').place(x=80, y=50)
271user_pass_text = Label(login, text='Password').place(x=220, y=50)
272
273
274
275
276# IP address input / delete
277Button(login, text='Add IP', width=10, command=addIP).place(x=160, y=280)
278Button(login, text='Delete IP', width=10, command=delOne).place(x=240, y=280)
279Label(login, text='Enter IP address').place(x=190, y=210)
280input_box_ip_address = Entry(login, width=21)
281list_box = Listbox(login, height=11, selectmode='SINGLE')
282
283list_box.place(x=25, y=210)
284input_box_ip_address.place(x=170, y=240)
285
286
287# -----------------------------------------------------------------------------
288
289retrieveDB()
290listUpdate()
291
292raise_frame(interface)
293
294while True:
295 root.mainloop()
296
297conn.commit()
298cur.close()
299
300
301
302
303
304
305
306
307
308C:\Users\Devtron\PycharmProjects\testme\venv\Scripts\python.exe C:/Users/Devtron/PycharmProjects/testme/test
309Traceback (most recent call last):
310 File "C:/Users/Devtron/PycharmProjects/testme/test", line 99, in <module>
311 render = PhotoImage(file = "controls.jpg")
312 File "C:\Users\Devtron\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3545, in __init__
313 Image.__init__(self, 'photo', name, cnf, master, **kw)
314 File "C:\Users\Devtron\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3501, in __init__
315 self.tk.call(('image', 'create', imgtype, name,) + options)
316_tkinter.TclError: couldn't recognize data in image file "controls.jpg"
317
318Process finished with exit code 1