#1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    won't save variables


    this program you are trying to guess the word "hello" by typing letters in the entry fields or clicking the buttons. then the letter goes through a loop to see if its in hello then saves it to result_var. i took alot of the stuff out so you can see the program easier.
    alright now the problem i am having is when you click on the "H" button it is suppose to go through the loop and then save to the result_var. before i had a setup where you clicked a letter and it added it to result_var. it looked like this.

    Code:
    def H_btn(self):
           allbefore = self.result_var.get()      
           allbefore = allbefore + "H"
           self.result_var.set(allbefore)
    i used the same thing but i saved H to a variable and sent it to another function. heres an example of the program.




    Code:
      
    from Tkinter import *
    word = ["H","E","L","L","O"]
    blanks = ["_","_","_","_","_"]
    len1 = len(word)
    correct = 0
    class App(Tk):
       def __init__(self, root):
           self.root = root
           root.geometry('600x200')
           c1 = "black"
           p1 = 2
           wd = 4
           self.typeguess = StringVar()
           
           Entry(root, textvariable = self.typeguess).grid(row = 7, column = 4, padx = p1, pady = p1, columnspan = 8)
           Label(root, text = "Pick a Letter ").grid(row = 7, column = 0, padx = p1, pady = p1, columnspan = 4)
           Button(root, text = "Go ", fg = c1, width = 3, command=self.go).grid(row = 7, column = 11, padx = p1, pady = p1, columnspan = 2)
    
           Button(root, text = "H", fg = c1, borderwidth = 3, relief = 'raised', width = wd, command=self.H_btn).grid(row = 4, column = 11, padx = p1, pady = p1, columnspan = 2)
           
           self.result_var = StringVar()
           self.result_var.set(' '.join(blanks))
           Label(root, textvariable=self.result_var).grid(row=1, column =1, columnspan = 20)
    
       def go(self):
           checkvar = self.typeguess.get()
           self.hg_run
           self.result_var.set(' '.join(blanks))
           self.typeguess.set('')
    
    
       def H_btn(self):
           checkvar = "H"
           self.hg_run
           self.result_var.set(' '.join(blanks))
    
       def hg_run(self, checkvar):
           correct = 0
           count1 = 0
           while count1 < len1:
               if checkvar.upper() == word[count1]:
                   global blanks
                   blanks[count1] = checkvar.upper()
                   correct = 1
               count1 == count1 + 1
    
       
    root = Tk()
    app  = App(root)
    root.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    You forgot to send checkvar to hg_run. I also changed the while-loop to a for-loop:
    Code:
    def H_btn(self):
        checkvar = "H"
        self.hg_run(checkvar)
        self.result_var.set(' '.join(blanks))
    
    def hg_run(self, checkvar):
        correct = 0
        for i in range(len1):
            if checkvar.upper() == word[i]:
                blanks[i] = checkvar.upper()
                correct = 1
    Regards, mawe
  4. #3
  5. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    Originally Posted by mawe
    Hi!

    You forgot to send checkvar to hg_run. I also changed the while-loop to a for-loop:
    Code:
    def H_btn(self):
        checkvar = "H"
        self.hg_run(checkvar)
        self.result_var.set(' '.join(blanks))
    
    def hg_run(self, checkvar):
        correct = 0
        for i in range(len1):
            if checkvar.upper() == word[i]:
                blanks[i] = checkvar.upper()
                correct = 1
    Regards, mawe

    ohh ok i see. a very novice mistake. thank you for your help. can you give a little explaination of the for loop? im not sure exactly how to use it or what it does.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Originally Posted by lw22
    can you give a little explaination of the for loop?
    Two examples say more than 1000 words
    Code:
    >>>for i in range(3):
    >>>    print i
    0
    1
    2
    >>>for i in ['a', 'b', 'c']:
    >>>    print i
    a
    b
    c
    Regards, mawe

IMN logo majestic logo threadwatch logo seochat tools logo