Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. This is a GUI built on Dylan5797's Scratch API
  2. '''
  3.  
  4. import tkinter as tk
  5. from tkinter import ttk
  6.  
  7. import scratchapi
  8.  
  9. LARGE_FONT = ("Verdana", 12)
  10.  
  11. class ScratchGUIApp(tk.Tk):
  12.  
  13. '''
  14. Main backend class, this is what makes stuff work.
  15. '''
  16.  
  17. def __init__(self, *args, **kwargs):
  18.  
  19. ''' Constructor '''
  20.  
  21. # Call the parents constructor
  22. tk.Tk.__init__(self, *args, **kwargs)
  23.  
  24. # Set the window title
  25. tk.Tk.wm_title(self, "Scratch GUI")
  26.  
  27. # Create the container
  28. self.container = tk.Frame(self)
  29. self.container.pack(side="top", fill="both", expand=True)
  30.  
  31. # And configure the grid
  32. self.container.grid_rowconfigure(0, weight=1)
  33. self.container.grid_columnconfigure(0, weight=1)
  34.  
  35. # Create a dictionary of frames and append all pages to it
  36. self.frames = {}
  37.  
  38. for f in (LoginPage, MainPage):
  39.  
  40. frame = f(self.container, self)
  41.  
  42. self.frames[f] = frame
  43.  
  44. frame.grid(row=0, column=0, sticky="nsew")
  45.  
  46. # Set the starting page
  47. self.show_frame(LoginPage)
  48.  
  49. def show_frame(self, cont):
  50.  
  51. # A simple function to switch pages
  52.  
  53. frame = self.frames[cont]
  54. frame.tkraise()
  55.  
  56. class LoginPage(tk.Frame):
  57.  
  58. '''
  59. This is all content on the login page
  60. '''
  61.  
  62. def __init__(self, parent, controller):
  63.  
  64. ''' Constructor '''
  65.  
  66. # Call the parents constructor
  67. tk.Frame.__init__(self, parent)
  68.  
  69. self.parent = parent
  70. self.controller = controller
  71.  
  72. # Add the title
  73. self.title = ttk.Label(self, text="Log in to your Scratch account", font=LARGE_FONT)
  74. self.title.grid(row=0,column=0,columnspan=2,pady=10)
  75.  
  76. # Add the login form
  77. self.usernameTag = ttk.Label(self, text="Username:")
  78. self.usernameTag.grid(row=1,column=0,sticky="e",pady=2)
  79. self.usernameEntry = ttk.Entry(self)
  80. self.usernameEntry.grid(row=1,column=1,pady=2)
  81. self.passwordTag = ttk.Label(self, text="Password:")
  82. self.passwordTag.grid(row=2,column=0,sticky="e",pady=2)
  83. self.passwordEntry = ttk.Entry(self)
  84. self.passwordEntry.grid(row=2,column=1,pady=2)
  85.  
  86. # Just in case theres anything to report
  87. self.errorMessage = ttk.Label(self, text="", foreground="red")
  88. self.errorMessage.grid(row=3,column=0,columnspan=2,pady=5)
  89.  
  90. # Add the disclaimer
  91. self.subtitle = ttk.Label(self, text="Account information is not collected in any way.")
  92. self.subtitle.grid(row=4,column=0,columnspan=2)
  93.  
  94. # And finally add the login button
  95. self.button = ttk.Button(self, text="Login",
  96. command= lambda: self.login())
  97. self.button.grid(row=5,column=0,columnspan=2,pady=10)
  98.  
  99. def login(self):
  100.  
  101. # Attempts to log the user in to the scratchapi
  102.  
  103. usernameData = self.usernameEntry.get()
  104. passwordData = self.passwordEntry.get()
  105.  
  106. if usernameData == "" or passwordData == "":
  107. # Stop the function if the fields are empty.
  108. self.errorMessage.config(text="These fields are required.")
  109. return
  110.  
  111. # Attempt to login to the scratchapi with the given username and password
  112.  
  113. try:
  114. scratch = scratchapi.ScratchUserSession(usernameData, passwordData)
  115. except:
  116. # Stop the function if there was an error
  117. self.errorMessage.config(text="Login failed.")
  118. return
  119.  
  120. self.controller.show_frame(MainPage)
  121.  
  122. class MainPage(tk.Frame):
  123.  
  124. '''
  125. This is all content on the main page.
  126. '''
  127.  
  128. def __init__(self, parent, controller):
  129.  
  130. ''' Constructor '''
  131.  
  132. # Call the parents constructor
  133. tk.Frame.__init__(self, parent)
  134.  
  135. self.parent = parent
  136. self.controller = controller
  137.  
  138. self.label=ttk.Label(text="hi")
  139. self.label.pack(in_=self)
  140.  
  141. app = ScratchGUIApp()
  142. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement