Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    10
    I created this program for you so I hope you find it useful

    PHP Code:
    import Tkinter
    import random

    class App:
        
    def __init__(self,lstWords,MaxRetries):
            
            
    self.tk_Root Tkinter.Tk()

            
    #Create a new Frame, bind key to function self.OnFrameEvent
            
    self.tk_Frame Tkinter.Frame(self.tk_Root)
            
    self.tk_Frame.bind("<Key>",self.OnFrameEvent)
            
    #Forces the focus to the main Frame, in order to recieve events
            
    self.tk_Frame.focus_force()

            
    #Just empty by default, generated later
            #Actual output, what the user intputted
            
    self.lst_Data = []

            
    #List of words
            
    self.lst_Words lstWords
            
            
    #The Word that will be guessed, this will be generated later using random.choice
            
    self.lst_Word = []

            
    #Maximum Retries, $$$Static$$$
            
    self.MaxRetries MaxRetries
            
            
    #The Output Label
            
    self.tk_Label Tkinter.Label(self.tk_Frame)

            
    #The Results/Counter
            #The variable int_Retries holds how many wrong retries you have done
            
    self.int_Retries 0
            
    #Here's it label
            
    self.tk_Results Tkinter.Label(self.tk_Frame)

            
    #Defines State of the game
            
    self.boolGameState False

            
    #Review comments on each function please
            #func_NewWord, assign self.lst_Word with a new randomly choosen word, and empty spaces for lst_Data
            
    self.func_NewWord()
            
    #Show lst_Data, after converting it to a string
            
    self.func_PackLabel()
            
    #Shows retries
            
    self.func_PackResults()

            
    #New Button, Using func_OnButton
            
    self.tk_Button Tkinter.Button(self.tk_Frame,text="New Word",command=self.func_OnButton)
            
    #I used grid instead of pack, for little more control on layout, anyway it doesn't matter just don't mix both
            
    self.tk_Button.grid(row=2)

            
    #pack the Frame object
            
    self.tk_Frame.grid()
            
        
    def func_PackResults(self):
            
    #Nothing special, packs the retries variable, with some text
            
    self.tk_Results['text'] = "Total Retries - " str(self.int_Retries)
            
    self.tk_Results.grid(row=1)
            
        
    def func_NewWord(self):
            
    #Using random.choice, it will pick one by random, and convert it into a list
            
    self.lst_Word = list( random.choice(self.lst_Words) )
            
    #Now self.lst_Data will hold _ for the size of the new random word
            
    self.lst_Data = ['_' for s in rangelen(self.lst_Word) ) ]

            
    #Reset counter/retries to 0
            
    self.int_Retries 0

            self
    .boolGameState=True
            
        def func_PackLabel
    (self):
            
    #Just a temproary variable holding the string of self.lst_Data
            
    str_Temp " ".join(self.lst_Data)
            
    #you can use self.tk_Label.configure(text=str_Temp) if you wish
            
    self.tk_Label['text']="Word - " str_Temp
            
    #Pack, just for safety
            
    self.tk_Label.grid(row=0)

        
    def func_OnButton(self):
            
    #Whenever you puch the new word button, this generates a new word and packs refresh word and retries labels
            
    self.func_NewWord()
            
    self.func_PackLabel()
            
    self.func_PackResults()
            
        
    def func_CompareChar(self,str_Char):
            
    #This function takes a character and searches self.lst_Word(The complete word)
            #if character is found, self.lst_Data (The Output) exact location will be assigned with same
            #Character
            
            #A Variable to see if a character was matched or not
            
    boolFound False
            
    for i in rangelenself.lst_Word ) ):
                if 
    self.lst_Word[i].lower()==str_Char.lower():
                    
    self.lst_Data[i] = str_Char
                    
    #If Character matchtes, Declare Found
                    
    boolFound=True
            
    #If no match, add 1 to retries
            
    if not boolFound:
                
    self.int_Retries+=1
                
        def CheckState
    (self):
            
    #Checks game state
            #if there's no "_" in word, and total retries are lower that Maximum retries, then you won
            
    if "_" not in self.lst_Data and self.int_Retries<self.MaxRetries:
                
    self.lst_Data = list("You Win")
                
    self.boolGameState=False
            
    #If your retries exceeds MaxRetries
            
    elif self.int_Retries>self.MaxRetries:
                
    self.lst_Data = list("You Lose")
                
    self.boolGameState=False
                
        def OnFrameEvent
    (self,event):
            
    #This is the event function, checks game state, Compares character and assign it throw func_CompareChar
            #Then packs the label, and results
            
    if self.boolGameState:
                
    self.func_CompareCharevent.char )
                
    self.CheckState()
                
    self.func_PackLabel()
                
    self.func_PackResults()
            
    WordsList = ['Apple','Orange','Banana','Box']
    MaxRetries 10

    Application 
    App(WordsListMaxRetries)
    Application.tk_Root.mainloop() 
  2. #17
  3. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    i know i need to learn tkinter more... im only 17 and started to look at any kind of programming language in January. so basically this is the first program i have made in my entire life. i thought i was doing pretty good but i guess not. i'll look at your program when i have time... thank you for making it
  4. #18
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I think you should practice it a little bit more before doing this program. Make something smaller to start with.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    10
    Originally Posted by lw22
    i know i need to learn tkinter more... im only 17 and started to look at any kind of programming language in January. so basically this is the first program i have made in my entire life. i thought i was doing pretty good but i guess not. i'll look at your program when i have time... thank you for making it
    you are doing pretty good because you're posting here and insisting to find a solution, this is what programming all about, just keep it up.
    I would give some comments, I don't think that starting with a GUI is a good way to start, because you must deal with specifications, more than programming concepts at some points, design console applications which is much more time efficient, and learn Visual Basic if you want some GUI

    anyway, I would really recommend that you save some time and start learning programming instead of programming languages, I mean discrete mathematics, algorithms and data structures are together an excellent way to start, seriously languages are just tools used by a programmer so if you understand the programming process, algorithms ..... you can learn any programming language in a few days.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo