Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Program Illusnist's Number Guess
- from tkinter import*
- import random
- #topWindow = None # Set global reference for root ## dont need this anymore
- #solution = 8 #or this
- class App:
- def __init__(self):
- self.topWindow = Tk() # creating the tkinter call
- self.solution = 8 #now a class based global variable.
- def stop(self):
- print("quitting")
- self.topWindow.destroy()#this is the better solution to using quit()
- def chkguess(self, x): #all functions in the app should have self as the first argument
- if x == self.solution: #everytime you want the solution variable just use self.solution
- print("You got it!")
- #quit() #this is bad practice
- self.reset()
- elif x > self.solution:
- print("Lower")
- elif x < self.solution:
- print("Higher")
- #return() #not needed
- ##this is more of a game
- def reset(self):
- self.solution = random.randint(0,11) #sets solution to random
- def run(self): #using run for you main function just looks better when using classes
- print("running run()")
- self.topWindow.title("Illusnist's Number Guesser") # Title Bar
- w = Label(self.topWindow, text="GUESS THE CORRECT NUMBER - 5 GUESSES") #again we are using self to determin its a global variable.
- w.grid() # Put the label into the window
- gamewin = Frame(self.topWindow)
- button1 = Button(gamewin, text="1", command=lambda i = 1:self.chkguess(i), width=2) #because the function is in the class use self.function()
- button1.grid(row=2, column=0)
- button2 = Button(gamewin, text="2", command=lambda i = 2:self.chkguess(i), width=2)
- button2.grid(row=2, column=1)
- button3 = Button(gamewin, text="3", command=lambda i = 3:self.chkguess(i), width=2)
- button3.grid(row=2, column=2)
- button4 = Button(gamewin, text="4", command=lambda i = 4:self.chkguess(i), width=2)
- button4.grid(row=3, column=0)
- button5 = Button(gamewin, text="5", command=lambda i = 5:self.chkguess(i), width=2)
- button5.grid(row=3, column=1)
- button6 = Button(gamewin, text="6", command=lambda i = 6:self.chkguess(i), width=2)
- button6.grid(row=3, column=2)
- button7 = Button(gamewin, text="7", command=lambda i = 7:self.chkguess(i), width=2)
- button7.grid(row=4, column=0)
- button8 = Button(gamewin, text="8", command=lambda i = 8:self.chkguess(i), width=2)
- button8.grid(row=4, column=1)
- button9 = Button(gamewin, text="9", command=lambda i = 9:self.chkguess(i), width=2)
- button9.grid(row=4, column=2)
- button10 = Button(gamewin, text="10", command=lambda i = 10:self.chkguess(i), width=2)
- button10.grid(row=5, column=1)
- #quitbutton = Button(gamewin, text="Exit", command=exit, width=2) #you have no exit function
- quitbutton = Button(gamewin, text="Exit", command=self.stop, width=2) #again using destroy
- quitbutton.grid(row=5, column=2)
- gamewin.grid()
- self.topWindow.mainloop() #did youi forget this ?
- # End setscreen()
- #def main(): #all of this is not needed
- #topWindow = Tk() # Main GUI window where all widgets go
- # topWindow.title("Illusnist's Number Guesser") # Title Bar
- # setscreen()
- #main()
- #better standard to use------------
- if __name__ == "__main__": #basicall translates to if i am running this python file and not calling it from another file.
- try:
- app = App() #creates instance of the class
- app.run()# runs the run() function inside the App class instance
- except KeyboardInterrupt: #press ctrl+c to trigger this
- print("quitting")
- app.stop()
- except:
- raise #simply output the error as normal.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement