Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    not saving as a list


    Hey.

    Alright while I'm in tkinter, I save a list to a self. variable(thats what I have to do in my original program). Then when I save the self. variable to another variable and print it, it appears to be a list. The only problem is it doesn't save as a list. Here is an example.

    Code:
    from Tkinter import *
    class App:
        def __init__(self, root):
            self.root = root
            self.blanks = StringVar()
            self.blanks.set(['_','_','_','_','_'])
            m = self.blanks.get()
            print m
    root = Tk()
    app  = App(root)
    root.mainloop()
    when it prints m it prints ['_', '_', '_', '_', '_']. now when i change print m to print [1], it prints a ' instead of a '_'.



    Edit- I just realized that it is probably because i saved self.blanks to a StringVar. Therefore it saves whatever i set in it as a string. I'm almost positive that is what the problem is... is there anyway to save it as a list?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Well what do you expect to receive? Are you testing this program in the interpreter? It is always going to print all of the data from a list value as a list only.
  4. #3
  5. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    my point is that it appears to be a list but it actually a string of all of that stuff. If i do len(m) it would come out to 25. if it was a list it would be 5.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Assuming you know a fair amount of Python functions. Did you try using the join() function? If you don't know how to use it or where to use it, I'll help you out.
    You may also be able to do the following:
    Code:
    for item in whatever_your_variable_is_valled:
        print item,
    I think that's correct.
  8. #5
  9. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    I know how to use join()... I'm not having trouble with that part. You are missing my problem. whenever i do
    Code:
    self.blanks.set(['_','_','_','_','_'])
    it saves as a string not a list. thats why when i print the length of self.blanks it returns 25. to my understanding it saves as a string because i saved self.blanks as a StringVar. I just tried to change everything in my real program to use strings but a string is immutable. i can just do self.blanks = ['_','_','_','_','_'] and it would save as a list but i need self.blanks to change... This sucks because it means my whole school project doesn't work... so much for getting an A.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Now that I know that this is for school. Is there a certain way this has to be done and that is why you are using StringVar()? Other than this, I'm not sure how else you would do this.
  12. #7
  13. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    well basically im making a hangman like program. i have it set up where the word you are suppose to find is a list ['H','I'] like that. Then i have blanks set up like this ['_','_']. in a nutshell... say the user inputs a letter like H. it then changes the blanks to ['H','_'] and displays that. i can't just use strings because they are immutable so i can't put H in for the _.

    now in tkinter the only way(i know of) that you can display something that changes is to use StingVar. since im going to need the blanks list to change i will need to save it to StringVar. when i save it to StringVar it saves as a string instead of a list. i need it to be a list so i can put the H in for the _.

    there are ways around this i guess. after you are done playing my game you are going to have to exit out and start it up again. this is only for high school and my teacher knows nothing about programming(its an independent study). hopefully she will think its good.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    Why using StringVar() at the first place why not simply
    self.blanks = ['_' for s in range(5) ]

    and for showing it as a string join(self.blanks,"")
    maybe something like self.strBlanks = lambda:join(self.blanks,"") #this is a function so use self.strBlanks()
  16. #9
  17. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    i've tried using self.blanks = ['_'... and so on. It works the first time around. after you finished self.blanks is going to have letters in it from the user's input. in my code i reset self.blanks to ['_'... but it stays the same with all of the letters in it. Therefor i came to the conclusion that i needed StringVar but that won't work either.

    Now i am going to have to use self.blanks = method and just have the program shut down after you use it.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    Originally Posted by lw22
    but it stays the same with all of the letters in it. .
    what do you mean by that ?

    You make it sounds too complicated, and it isn't
    why not send your code over here?
  20. #11
  21. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    because the code is kinda complicated... basically this is what i got
    Code:
    self.word = ['H','I'] #what the word is
    self.blanks = ['_','_'] # the blanks...
    #if the user inputs H it changes self blanks to 
    self.blanks = ['H','_']
    #when the program is over it resets self.blanks to the original
    self.blanks = ['_','_']
    #however it doesn't reset self.blanks to this. self.blanks stays at ['H','I']
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    Originally Posted by lw22
    #when the program is over it resets self.blanks to the original
    self.blanks = ['_','_']
    #however it doesn't reset self.blanks to this. self.blanks stays at ['H','I'][/code]
    for sure you forget to pack your label
  24. #13
  25. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    positive... i printed the value out on the screen. copy and run this through python.
    Code:
    from Tkinter import *
    class App:
        def __init__(self, root):
            self.root = root
            self.blanks = ['H','I']
            Label(root, text = self.blanks).pack(side=LEFT)
            Button(root, text = "Change", command = self.bye).pack(side=LEFT)
        def bye(self):
            self.blanks = ['B','Y','E']
    root = Tk()
    app  = App(root)
    root.mainloop()
    when you click change it does nothing. to make it change you need to use StringVar(). like this.

    Code:
    from Tkinter import *
    class App:
        def __init__(self, root):
            self.root = root
            self.blanks = StringVar()
            self.blanks.set(['H','I'])
            Label(root, textvariable = self.blanks).pack(side=LEFT)
            Button(root, text = "Change", command = self.bye).pack(side=LEFT)
        def bye(self):
            self.blanks.set(['B','Y','E'])
    root = Tk()
    app  = App(root)
    root.mainloop()
    now it makes the change but converts my list to a string.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    I don't really understand why you still have a problem with this Doesn't the code I showed you in the PM work? We used join there
    Code:
    self.result_var.set(' '.join(self.blanks))
    So whats the problem? The blanks are still a list. And no, you don't have to use a StringVar(). You can also use configure().
    Code:
    from Tkinter import *
    
    class App:
        def __init__(self, root):
            self.root = root
            self.text = 'HELLO'
            self.blanks = ['_']*len(self.text)
            self.l = Label(root, text=' '.join(self.blanks))
            self.l.pack(side=LEFT)
            Button(root, text="L", command=self.change).pack(side=LEFT)
    
        def change(self):
            for i,char in enumerate(self.text):
                if char == 'L':
                    self.blanks[i] = 'L'
            self.l.configure(text=' '.join(self.blanks))
    
    root = Tk()
    App(root)
    root.mainloop()
    Regards, mawe
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    73
    Rep Power
    11
    you need to learn Tkinter more

    in order to change a Tkinter object, this object must be stored in a variable( this is why variables are used in programming )

    so instead of Label(root,te....., you must store it in a self.$$$ variable
    second and most important do not assign a list to text, you should convert it to a string first as stated early, join(self.blanks,"")

    here's an example of what I'm talking about
    Code:
    self.label = Label(root, text="".join(self.blanks))
    self.label.pack()
    
    def bye(self):
         self.blanks =  ['B','Y','E']
         self.label['text']="".join(self.blanks)
         self.label.pack()
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo