Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql
- import tkinter as tk # python 3
- from tkinter import font as tkfont # python 3
- #import Tkinter as tk # python 2
- #import tkFont as tkfont # python 2
- class SampleApp(tk.Tk):
- def __init__(self, *args, **kwargs):
- tk.Tk.__init__(self, *args, **kwargs)
- self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
- # the container is where we'll stack a bunch of frames
- # on top of each other, then the one we want visible
- # will be raised above the others
- container = tk.Frame(self)
- container.pack(side="top", fill="both", expand=True)
- container.grid_rowconfigure(0, weight=1)
- container.grid_columnconfigure(0, weight=1)
- self.frames = dict()
- for F in (StartPage, PageOne):
- page_name = F.__name__
- frame = F(parent=container, controller=self)
- self.frames[page_name] = frame
- # put all of the pages in the same location;
- # the one on the top of the stacking order
- # will be the one that is visible.
- # frame.grid(row=0, column=0, sticky="nsew")
- self.show_frame("StartPage")
- self.show_frame("PageOne")
- def show_frame(self, page_name):
- '''Show a frame for the given page name'''
- frame = self.frames[page_name]
- frame.tkraise()
- menu_bar = frame.menu_bar(self)
- self.configure(menu=menu_bar)
- sampleapp = SampleApp()
- showframe = 'show_frame'
- method = getattr(sampleapp, showframe)
- method()
- class StartPage(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- self.label_1 = tk.Label(parent, text="Username:")
- self.label_2 = tk.Label(parent, text="Password:")
- self.username = tk.Entry(parent, textvariable=tk.StringVar())
- self.username.focus_set()
- self.password = tk.Entry(parent, show="*", textvariable=tk.StringVar())
- self.sign_in = tk.Button(parent, text="Sign In", command=lambda: self.validation(method()))
- self.label_1.grid(row=0)
- self.label_2.grid(row=1)
- self.username.grid(row=0, column=1, padx=3, pady=2)
- self.password.grid(row=1, column=1)
- self.sign_in.grid(row=2, column=1, rowspan=2)
- def menu_bar(self, root):
- main_menu = tk.Menu(root)
- def validation(self, controller):
- u = str(self.username.get())
- p = str(self.password.get())
- conn = pymysql.connect(host="localhost", user="root", password="", db="e-library")
- c = conn.cursor()
- verify_user = "SELECT * from tblusers WHERE username = %s AND password = %s"
- c.execute(verify_user, (u, p))
- results = c.fetchall()
- if results:
- for i in results:
- print("Welcome " + i[1])
- else:
- print("Username or Password not recognized")
- class PageOne(tk.Frame):
- def __init__(self, parent, controller):
- self.controller = controller
- self.parent = parent
- def menu_bar(self, controller):
- main_menu = tk.Menu(controller)
- file_menu = tk.Menu(main_menu)
- new_menu = tk.Menu(file_menu)
- main_menu.add_cascade(label='File', menu=file_menu)
- file_menu.add_cascade(label='New', menu=new_menu)
- file_menu.add_command(label='Register Student...', command=self.new_student)
- file_menu.add_command(label='Register Journal', menu=self.new_journal)
- file_menu.add_command(label='Register Journal', command=self.new_journal)
- return main_menu
- def new_student(self):
- print('I am a student')
- def new_journal(self):
- print('I am a journal')
- '''
- class PageTwo(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- label = tk.Label(self, text="This is page 2", font=controller.title_font)
- label.pack(side="top", fill="x", pady=10)
- button = tk.Button(self, text="Go to the start page",
- command=lambda: controller.show_frame("StartPage"))
- button.pack()
- '''
- if __name__ == "__main__":
- app = SampleApp()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement