number guessing game python gui

Solutions on MaxInterview for number guessing game python gui by the best coders in the world

showing results for - "number guessing game python gui"
Annabelle
07 Jun 2017
1# guess the number in a GUI
2
3from tkinter import *
4import random
5
6
7class Application(Frame):
8    ''' GUI guess the number application  '''
9    def __init__(self, master):
10        '''initialise frame'''
11        super(Application, self).__init__(master)
12        self.grid()
13        self.create_widgets()
14
15
16    def create_widgets(self):
17        '''create widgets for GUI guess game'''
18        #Create title label
19        Label(self, text = 'Guess the number'
20              ).grid(row = 0, column = 1, columnspan = 2, sticky = N)
21        # create instruction labels
22        Label(self, text = 'I am thinking of a number between 1 and 100'
23              ).grid(row = 1, column = 0, columnspan = 3, sticky = W)
24
25        Label(self, text = 'Try to guess in as few attempts as possible'
26              ).grid(row = 2, column = 0, columnspan = 3, sticky = W)
27
28        Label(self, text = 'I will tell you to go higher or lower after each guess'
29              ).grid(row = 3, column = 0, columnspan = 3, sticky = W)
30        # Create a label and number entry
31        Label(self, text = 'Your guess: '
32              ).grid(row = 4, column = 0, sticky = W)
33        self.guess_ent = Entry(self)
34        self.guess_ent.grid(row = 4, column = 1, sticky = W)
35        # create label and text box for number of tries
36        Label(self, text = ' number of tries: '
37              ).grid(row = 5, column = 0, sticky = W)
38        self.no_tries_txt = Text(self, width = 2, height = 1, wrap = NONE)
39        self.no_tries_txt.grid(row = 5, column = 1, sticky = W)
40
41        # create guess button
42
43        Button(self, text = 'Guess', command = self.check_if_correct
44               ).grid(row = 5, column = 2, sticky = W)
45
46        self.result_txt = Text(self, width = 80, height = 15, wrap = WORD)
47        self.result_txt.grid(row = 6, column = 0, columnspan = 4)
48
49        # display the results
50        #self.result_txt.delete(0.0, END)
51        #self.result_txt.insert(0.0, display_result)
52        #self.no_tries_txt.delete(0.0, END)
53        #self.no_tries_txt.insert(0.0, the_number)
54
55    def rnumber(self):
56        self.rnumber = random.randint(1, 100)
57
58
59    def check_if_correct(self):
60
61        self.result = ""
62        gnum = self.guess_ent.get()
63        gnum = int(gnum)
64
65        if gnum == self.rnumber:
66            gnum = str(gnum)
67            self.result = gnum + " is the magic number!\n"
68        elif gnum < self.rnumber:
69            gnum = str(gnum)
70            self.result = gnum + " is too low.\n"
71        elif gnum > self.rnumber:
72            gnum = str(gnum)
73            self.result= gnum + " is too high.\n"
74
75
76    def giveResult(self):
77        return str(self.result)
78
79
80        # display the results
81        self.result_txt.delete(0.0, END)
82        self.result_txt.insert(0.0, result)
83        #self.no_tries_txt.delete(0.0, END)
84        #self.no_tries_txt.insert(0.0, the_number)
85
86
87# main
88root = Tk()
89root.title('Guess the Number')
90app = Application(root)
91root.mainloop()
92