--The operation of each DB was written out as def. --root = tk.Tk () Display system from the following ――It feels like it's well organized.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
import pyotp
import sqlite3
import os
def create_table():
#Connect to database
conn = sqlite3.connect('gauth.db')
c = conn.cursor()
#Creating a table
sql='''CREATE TABLE gauth
(id integer primary key AUTOINCREMENT,
name text,
private_key text)'''
c.execute(sql)
#Save (commit) the inserted result
conn.commit()
#Close when you have finished accessing the database
conn.close()
def view():
#Connect to database
conn = sqlite3.connect('gauth.db')
c = conn.cursor()
for a in c.execute("select * from gauth"):
totp = pyotp.TOTP(a[2]) #Key value
totp.now()
#Label for display
Static1 = tk.Label(text=a[1])
Static1.pack(side='left')
Static2 = tk.Label(text=totp.now())
Static2.pack(side='left')
#Save (commit) the inserted result
conn.commit()
#Close when you have finished accessing the database
conn.close()
def insert(id,username,private_key):
#Connect to database
conn = sqlite3.connect('gauth.db')
c = conn.cursor()
#Insert data
c.execute(
"INSERT INTO gauth VALUES (?,?,?)",(id,username,private_key))
#Save (commit) the inserted result
conn.commit()
#Close when you have finished accessing the database
conn.close()
#Create window and title with tkinter
#Specify window size
root = tk.Tk()
root.title(u"g_authentication_tool")
root.geometry("300x200")
#menu bar
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label="sign up")
filemenu.add_command(label="Delete registration")
filemenu.add_command(label="close")
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
if(os.path.exists('gauth.db')):
view()
else:
create_table()
#insert(1,"user1",'base32secret3232')
view()
root.mainloop()
Recommended Posts