Advertisement
Guest User

Method problems

a guest
Jan 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.83 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 (LoginPage, AdminPage):
  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("LoginPage")
  35.         self.show_frame("AdminPage")
  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. class LoginPage(tk.Frame):
  47.  
  48.     def __init__(self, parent, controller):
  49.         tk.Frame.__init__(self, parent)
  50.         self.controller = controller
  51.         self.label_1 = tk.Label(parent, text="Username:")
  52.         self.label_2 = tk.Label(parent, text="Password:")
  53.         self.username = tk.Entry(parent, textvariable=tk.StringVar())
  54.         self.username.focus_set()
  55.         self.password = tk.Entry(parent, show="*", textvariable=tk.StringVar())
  56.         self.sign_in = tk.Button(parent, text="Sign In", command=lambda: self.validation(self.controller))
  57.         self.label_1.grid(row=0)
  58.         self.label_2.grid(row=1)
  59.         self.username.grid(row=0, column=1)
  60.         self.password.grid(row=1, column=1)
  61.         self.sign_in.grid(row=2, column=1, rowspan=2)
  62.  
  63.     def menu_bar(self, root):
  64.         main_menu = tk.Menu(root)
  65.  
  66.     def validation(self, controller):
  67.         u = str(self.username.get())
  68.         p = str(self.password.get())
  69.         conn = pymysql.connect(host="localhost", user="root", password="", db="e-library")
  70.         c = conn.cursor()
  71.         verify_user = "SELECT * from tblusers WHERE username = %s AND password = %s"
  72.         c.execute(verify_user, (u, p))
  73.         results = c.fetchall()
  74.  
  75.         if results:
  76.             for i in results:
  77.                 print("Welcome " + i[1])
  78.         else:
  79.             print("Username or Password not recognized")
  80.  
  81.  
  82. class AdminPage(tk.Frame):
  83.  
  84.     def __init__(self, parent, controller):
  85.         tk.Frame.__init__(self, parent)
  86.         self.controller = controller
  87.         self.parent = parent
  88.  
  89.     def menu_bar(self, controller):
  90.         main_menu = tk.Menu(controller)
  91.         file_menu = tk.Menu(main_menu)
  92.         new_menu = tk.Menu(file_menu)
  93.         main_menu.add_cascade(label='File', menu=file_menu)
  94.         file_menu.add_cascade(label='New', menu=new_menu)
  95.         file_menu.add_command(label='Register Student...', command=self.new_student)
  96.         file_menu.add_command(label='Register Journal', command=self.new_journal)
  97.         return main_menu
  98.  
  99.     def new_student(self):
  100.         print('I am a student')
  101.  
  102.     def new_journal(self):
  103.         print('I am a journal')
  104.  
  105.  
  106. sample_app = SampleApp()
  107. show_frame = 'show_frame'
  108. method = getattr(sample_app, show_frame)
  109. method()
  110.  
  111.  
  112. if __name__ == "__main__":
  113.     app = SampleApp()
  114.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement