――I will also post the one that has failed. ――This time, I cleaned the main operation part. --I modified it to operate the newly registered part from the menu at the top. --Menu> You can now close.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
import pyotp
import sqlite3
import os
import pyautogui
import sys
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()
def window_menu():
#menu bar
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label="sign up", command=new)
filemenu.add_command(label="Delete registration")
filemenu.add_command(label="close", command=close)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
def new():
pyautogui.prompt(text='user_name', title='user_name' , default='')
pyautogui.prompt(text='private_key', title='private_key' , default='')
#insert(user_name,private_key)
def close():
sys.exit()
#Create window and title with tkinter
#Specify window size
root = tk.Tk()
root.title(u"g_authentication_tool")
root.geometry("300x200")
window_menu()
if(os.path.exists('gauth.db')):
view()
else:
create_table()
#insert(1,"user1",'base32secret3232')
view()
root.mainloop()
Recommended Posts