Advertisement
AceScottie

number game

Aug 2nd, 2018
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. # Program Illusnist's Number Guess
  2. from tkinter import*
  3. import random
  4. #topWindow = None  # Set global reference for root ## dont need this anymore
  5.  
  6. #solution = 8 #or this
  7. class App:
  8.     def __init__(self):
  9.         self.topWindow = Tk() # creating the tkinter call
  10.         self.solution = 8 #now a class based global variable.
  11.    
  12.     def stop(self):
  13.         print("quitting")
  14.         self.topWindow.destroy()#this is the better solution to using quit()
  15.  
  16.     def chkguess(self, x): #all functions in the app should have self as the first argument
  17.         if x == self.solution: #everytime you want the solution variable just use self.solution
  18.             print("You got it!")
  19.             #quit() #this is bad practice
  20.             self.reset()
  21.         elif x > self.solution:
  22.             print("Lower")
  23.         elif x < self.solution:
  24.             print("Higher")
  25.         #return() #not needed
  26.        
  27.     ##this is more of a game
  28.     def reset(self):
  29.         self.solution = random.randint(0,11) #sets solution to random
  30.      
  31.      
  32.     def run(self): #using run for you main function just looks better when using classes
  33.         print("running run()")
  34.         self.topWindow.title("Illusnist's Number Guesser")  # Title Bar
  35.         w = Label(self.topWindow, text="GUESS THE CORRECT NUMBER - 5 GUESSES") #again we are using self to determin its a global variable.
  36.         w.grid()  # Put the label into the window
  37.         gamewin = Frame(self.topWindow)
  38.  
  39.         button1 = Button(gamewin, text="1", command=lambda i = 1:self.chkguess(i), width=2) #because the function is in the class use self.function()
  40.         button1.grid(row=2, column=0)
  41.         button2 = Button(gamewin, text="2", command=lambda i = 2:self.chkguess(i), width=2)
  42.         button2.grid(row=2, column=1)
  43.         button3 = Button(gamewin, text="3", command=lambda i = 3:self.chkguess(i), width=2)
  44.         button3.grid(row=2, column=2)
  45.         button4 = Button(gamewin, text="4", command=lambda i = 4:self.chkguess(i), width=2)
  46.         button4.grid(row=3, column=0)
  47.         button5 = Button(gamewin, text="5", command=lambda i = 5:self.chkguess(i), width=2)
  48.         button5.grid(row=3, column=1)
  49.         button6 = Button(gamewin, text="6", command=lambda i = 6:self.chkguess(i), width=2)
  50.         button6.grid(row=3, column=2)
  51.         button7 = Button(gamewin, text="7", command=lambda i = 7:self.chkguess(i), width=2)
  52.         button7.grid(row=4, column=0)
  53.         button8 = Button(gamewin, text="8", command=lambda i = 8:self.chkguess(i), width=2)
  54.         button8.grid(row=4, column=1)
  55.         button9 = Button(gamewin, text="9", command=lambda i = 9:self.chkguess(i), width=2)
  56.         button9.grid(row=4, column=2)
  57.         button10 = Button(gamewin, text="10", command=lambda i = 10:self.chkguess(i), width=2)
  58.         button10.grid(row=5, column=1)
  59.        
  60.         #quitbutton = Button(gamewin, text="Exit", command=exit, width=2) #you have no exit function
  61.         quitbutton = Button(gamewin, text="Exit", command=self.stop, width=2) #again using destroy
  62.         quitbutton.grid(row=5, column=2)
  63.    
  64.         gamewin.grid()
  65.         self.topWindow.mainloop() #did youi forget this ?
  66.         # End setscreen()
  67.  
  68.     #def main(): #all of this is not needed
  69.         #topWindow = Tk() # Main GUI window where all widgets go
  70.     #   topWindow.title("Illusnist's Number Guesser")  # Title Bar
  71.     #   setscreen()
  72.  
  73. #main()
  74.  
  75. #better standard to use------------
  76. if __name__ == "__main__": #basicall translates to if i am running this python file and not calling it from another file.
  77.     try:
  78.         app = App() #creates instance of the class
  79.         app.run()# runs the run() function inside the App class instance
  80.     except KeyboardInterrupt: #press ctrl+c to trigger this
  81.         print("quitting")
  82.         app.stop()
  83.     except:
  84.         raise #simply output the error as normal.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement