· 9 years ago · Nov 10, 2016, 12:58 PM
1from tkinter import *
2import sqlite3
3import GPIO
4
5def mainWindow(): #Functie voor het hoofdscherm
6 global nameEntry, passwordEntry
7 tabelAanmaken()
8 startWindow = Tk()
9 startWindow.wm_title("Alarm") #Window Alarm aanmaken
10 startWindow.geometry("300x100")
11 startWindow.resizable(width=False, height=False) #Size van window locken
12 L1 = Label(startWindow, text="Gebruikersnaam:") #Labels om gegevens in te vullen
13 L1.place(x=30,y=10)
14 L2 = Label(startWindow, text="Wachtwoord:")
15 L2.place(x=30,y=35)
16 nameEntry = Entry(startWindow, bd=5)
17 nameEntry.place(x=150,y=10)
18 passwordEntry = Entry(startWindow, bd=5)
19 passwordEntry.place(x=150,y=35)
20 loginButton = Button(startWindow, text="Login", width=20, command=loginCheck)
21 loginButton.place(x=80, y=70) #Button om in te loggen
22 startWindow.mainloop() #Start de window Alarm
23
24def loginCheck():
25 password = passwordEntry.get()
26 con = sqlite3.connect('alarmdatabase.sql')
27 cur = con.cursor()
28 cur.execute("select wachtwoord from gebruikers where wachtwoord=?", (password,))
29 data = cur.fetchall()
30 if not data:
31 pass
32 else:
33 menuWindow = Tk()
34 menuWindow.wm_title("Menu") #Window Menu aanmaken
35 menuWindow.resizable(width=False, height=False) #Size van window locken
36 aanpassenButton = Button(menuWindow, text="Gegevens aanpassen", width=20, command=aanpassen)
37 startButton = Button(menuWindow, text="Start", width=20, command=GPIO.startCheck)
38 alarmButton = Button(menuWindow, text="Alarm", width=20, command=GPIO.alarmTrigger)
39 stopButton = Button(menuWindow, text="Stop", width=20, command=GPIO.stopCheck)
40 aanpassenButton.pack()
41 startButton.pack()
42 stopButton.pack()
43 alarmButton.pack()
44
45def aanpassen():
46 global nieuwidEntry
47 global nieuwemailEntry
48 changeWindow = Tk()
49 changeWindow.wm_title("Aanpassen") #Window Aanpassen aanmaken
50 changeWindow.geometry("300x100")
51 changeWindow.resizable(width=False, height=False)
52 L1 = Label(changeWindow, text="Nieuw Telegram ID:") #Label om nieuw telegram ID in te vullen
53 L2 = Label(changeWindow, text="Nieuw emailadres:")
54 L1.place(x=30,y=10)
55 L2.place(x=30,y=40)
56 nieuwidEntry = Entry(changeWindow, bd=5)
57 nieuwemailEntry = Entry(changeWindow, bd=5)
58 nieuwidEntry.place(x=150,y=10)
59 nieuwemailEntry.place(x=150,y=40)
60 regButton = Button(changeWindow, text="Oke", width=20,command=invoerendata)
61 regButton.place(x=80, y=70)
62 changeWindow.mainloop() #Start de window Aanpassen
63
64######VANAF HIER ALLES MET DATABASE
65def tabelAanmaken(): #Funtie om table aan te maken
66 connection = sqlite3.connect("alarmdatabase.sql")
67 cursor = connection.cursor() #Connector en Cursor aanmaken
68 sql_command = """
69 CREATE TABLE gebruikers (
70 gebruikersnaam VARCHAR(50),
71 wachtwoord VARCHAR(50));"""
72 cursor.execute('drop table if exists gebruikers')
73 try:
74 cursor.execute(sql_command) #Kolommen voor in de tabel
75 except sqlite3.OperationalError:
76 pass
77 sql_command = """
78 CREATE TABLE ids (
79 telegramid VARCHAR(10),
80 email VARCHAR(50));"""
81 try:
82 cursor.execute(sql_command) #Kolommen voor in de tabel
83 except sqlite3.OperationalError:
84 pass
85 connection.commit()
86 connection.close() #Opslaan en sluiten
87 admininvoeren()
88
89def admininvoeren():
90 connection = sqlite3.connect("alarmdatabase.sql")
91 cursor = connection.cursor() #Connector en cursor aanmaken
92 sql_command = """INSERT INTO gebruikers (gebruikersnaam, wachtwoord)
93 VALUES ("admin", "8ks98Ab1H");"""
94 cursor.execute(sql_command)
95 connection.commit()
96 connection.close()
97
98def invoerendata():
99 telegramid = nieuwidEntry.get()
100 email = nieuwemailEntry.get()
101 connection = sqlite3.connect("alarmdatabase.sql")
102 cursor = connection.cursor() #Connector en cursor aanmaken
103 cursor.execute("DELETE FROM ids;")
104 sql_command = """INSERT INTO ids (telegramid)
105 VALUES ("{}");"""
106 sql_command = sql_command.format(telegramid)
107 cursor.execute(sql_command)
108 sql_command = """INSERT INTO ids (email)
109 VALUES ("{}");"""
110 sql_command = sql_command.format(email)
111 cursor.execute(sql_command)
112 connection.commit()
113 connection.close()
114
115def telegramCheck():
116 ##Telegramid
117 con = sqlite3.connect('alarmdatabase.sql')
118 cur = con.cursor()
119 cur.execute("select telegramid from ids")
120 data = cur.fetchall()
121 return data
122
123def emailCheck():
124 ##Email
125 con = sqlite3.connect('alarmdatabase.sql')
126 cur = con.cursor()
127 cur.execute("select email from ids")
128 data = cur.fetchall()
129 return data