Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

won't save variable

Status
Not open for further replies.

lw22

Programmer
Feb 19, 2005
7
0
0
US
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()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top