Advertisement
Guest User

NameError

a guest
Jan 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. import pymysql
  2. import tkinter as tk                # python 3
  3. from tkinter import font as tkfont # python 3
  4. #import Tkinter as tk     # python 2
  5. #import tkFont as tkfont  # python 2
  6.  
  7.  
  8. class SampleApp(tk.Tk):
  9.  
  10.     def __init__(self, *args, **kwargs):
  11.         tk.Tk.__init__(self, *args, **kwargs)
  12.  
  13.         self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
  14.  
  15.         # the container is where we'll stack a bunch of frames
  16.         # on top of each other, then the one we want visible
  17.         # will be raised above the others
  18.         container = tk.Frame(self)
  19.         container.pack(side="top", fill="both", expand=True)
  20.         container.grid_rowconfigure(0, weight=1)
  21.         container.grid_columnconfigure(0, weight=1)
  22.  
  23.         self.frames = dict()
  24.         for F in (StartPage, PageOne):
  25.             page_name = F.__name__
  26.             frame = F(parent=container, controller=self)
  27.             self.frames[page_name] = frame
  28.  
  29.             # put all of the pages in the same location;
  30.             # the one on the top of the stacking order
  31.             # will be the one that is visible.
  32.             # frame.grid(row=0, column=0, sticky="nsew")
  33.  
  34.         self.show_frame("StartPage")
  35.         self.show_frame("PageOne")
  36.  
  37.     def show_frame(self, page_name):
  38.         '''Show a frame for the given page name'''
  39.         frame = self.frames[page_name]
  40.         frame.tkraise()
  41.  
  42.         menu_bar = frame.menu_bar(self)
  43.         self.configure(menu=menu_bar)
  44.  
  45.  
  46. sampleapp = SampleApp()
  47. showframe = 'show_frame'
  48. method = getattr(sampleapp, showframe)
  49. method()
  50.  
  51.  
  52. class StartPage(tk.Frame):
  53.  
  54.     def __init__(self, parent, controller):
  55.         tk.Frame.__init__(self, parent)
  56.         self.controller = controller
  57.         self.label_1 = tk.Label(parent, text="Username:")
  58.         self.label_2 = tk.Label(parent, text="Password:")
  59.         self.username = tk.Entry(parent, textvariable=tk.StringVar())
  60.         self.username.focus_set()
  61.         self.password = tk.Entry(parent, show="*", textvariable=tk.StringVar())
  62.         self.sign_in = tk.Button(parent, text="Sign In", command=lambda: self.validation(method()))
  63.         self.label_1.grid(row=0)
  64.         self.label_2.grid(row=1)
  65.         self.username.grid(row=0, column=1, padx=3, pady=2)
  66.         self.password.grid(row=1, column=1)
  67.         self.sign_in.grid(row=2, column=1, rowspan=2)
  68.  
  69.     def menu_bar(self, root):
  70.         main_menu = tk.Menu(root)
  71.  
  72.     def validation(self, controller):
  73.         u = str(self.username.get())
  74.         p = str(self.password.get())
  75.         conn = pymysql.connect(host="localhost", user="root", password="", db="e-library")
  76.         c = conn.cursor()
  77.         verify_user = "SELECT * from tblusers WHERE username = %s AND password = %s"
  78.         c.execute(verify_user, (u, p))
  79.         results = c.fetchall()
  80.  
  81.         if results:
  82.             for i in results:
  83.                 print("Welcome " + i[1])
  84.         else:
  85.             print("Username or Password not recognized")
  86.  
  87.  
  88. class PageOne(tk.Frame):
  89.  
  90.     def __init__(self, parent, controller):
  91.         self.controller = controller
  92.         self.parent = parent
  93.  
  94.     def menu_bar(self, controller):
  95.         main_menu = tk.Menu(controller)
  96.         file_menu = tk.Menu(main_menu)
  97.         new_menu = tk.Menu(file_menu)
  98.         main_menu.add_cascade(label='File', menu=file_menu)
  99.         file_menu.add_cascade(label='New', menu=new_menu)
  100.         file_menu.add_command(label='Register Student...', command=self.new_student)
  101.         file_menu.add_command(label='Register Journal', menu=self.new_journal)
  102.         file_menu.add_command(label='Register Journal', command=self.new_journal)
  103.         return main_menu
  104.  
  105.     def new_student(self):
  106.         print('I am a student')
  107.  
  108.     def new_journal(self):
  109.         print('I am a journal')
  110.  
  111. '''
  112. class PageTwo(tk.Frame):
  113.  
  114.    def __init__(self, parent, controller):
  115.        tk.Frame.__init__(self, parent)
  116.        self.controller = controller
  117.        label = tk.Label(self, text="This is page 2", font=controller.title_font)
  118.        label.pack(side="top", fill="x", pady=10)
  119.        button = tk.Button(self, text="Go to the start page",
  120.                           command=lambda: controller.show_frame("StartPage"))
  121.        button.pack()
  122. '''
  123.  
  124. if __name__ == "__main__":
  125.     app = SampleApp()
  126.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement