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

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    reset tkinter variable (probably need class help)


    alright well heres the problem... i import a list of numbers as 'a'. when you click 'new game' it saves the list of numbers as self.inVarlist. now the user can click certain buttons and it changes self.inVarlist. if the user wants to click new game it should save the list of numbers to self.inVarlist so it would be like the first time you clicked new game. instead it doesn't change anything at all. here is my code for new game.

    Code:
        def newgame(self): #starts game
            self.inVarlist = a #saves list of 82 numbers to self.inVarlist
            for i in range(1,82):
                if self.inVarlist[i] != 0:
                    self.rbutton[i].configure(text = self.inVarlist[i])
                    self.locklist[i]=3
                else:self.rbutton[i].configure(text = '')
    im thinking that i have to delete self.inVarlist and self.locklist then create them again. (self.locklist is basically the same thing). if so how would i go about doing that?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Maybe you could complete some of your code, so to understand better what you're trying to do...
  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 danf_1979
    Maybe you could complete some of your code, so to understand better what you're trying to do...
    ok sorry... i thought that would be enough. here it is.

    Code:
    from Tkinter import *
    a=['c',0,0,9,2,0,0,0,0,0,1,2,0,3,0,0,0,4,0,6,0,0,9,1,0,0,8,0,7,1,0,0,0,0,0,0,0,2,0,0,8,0,7,0,0,3,0,0,0,0,0,0,0,2,7,0,5,0,0,6,8,0,0,9,0,7,0,0,0,3,0,1,8,0,0,0,0,0,2,7,0,0]
    class App:
        def __init__(self, root):
            self.root = root
            self.inVarlist = ['c'] #a list of all numbers used
            self.rbutton = ["c"]  #9x9 Grid
            self.numbutton = ["c"] #numbers 1-9
            self.locklist = ["c"] #Block is locked/unlocked
            self.rb = IntVar()  #9x9 Grid Block variable
            self.nb = IntVar() #numbers 1-9 variable
            self.rb.set(1)
            self.nb.set(1)
            clrs = ['#66FFCC','#00FF00','#66FFCC','#00FF00','#66FFCC','#00FF00','#66FFCC','#00FF00','#66FFCC']
            row,col,x,y=0,0,0,1
            for index in range(1,82): #makes 9x9 grid of radiobuttons
                self.inVarlist.append(0)
                self.locklist.append(0)
                self.rbutton.append(Radiobutton(root, text='', variable=self.rb, value=index, width=3, height=1, indicatoron=0, bg=clrs[x]))
                self.rbutton[-1].grid(row=row, column=col)
                col+=1
                if index%9==0: row+=1; col=0;
                if index%3==0:
                    x+=1
                    y+=1
                    if y == 4: x=0
                    if y == 7: x=0
                    if y == 13: x=3
                    if y == 16: x=3
                    if y == 22: x=6
                    if y == 25: x=6
            row2=0
            for index in range(1,10): #makes 9 radio buttons for numbers
                self.numbutton.append(Radiobutton(root, text=index, variable=self.nb, value=index, indicatoron=0, width=3, height=1))
                self.numbutton[-1].grid(row=row2, column=9,sticky=W)
                row2+=1
            Button(root, text='Enter', command=self.place).grid(row=9, column=8, columnspan=2)
            Button(root, text="Lock/Unlock Block", command=self.lock).grid(row=9, column=0, columnspan=4)
            Button(root, text="New Game", command=self.newgame).grid(row=9, column=4, columnspan=3)
            Button(root, text="Del", command=self.delete).grid(row=9, column=7)
            root.bind("<Return>", self.place)
            for i in range(1,10):
                root.bind(str(i), self.numbers)
            root.bind("<Up>", self.upkey)
            root.bind("<Down>", self.downkey)
            root.bind("<Left>", self.leftkey)
            root.bind("<Right>", self.rightkey)
        def newgame(self): #starts game
            self.inVarlist = a
            for i in range(1,82):
                if self.inVarlist[i] != 0:
                    self.rbutton[i].configure(text = self.inVarlist[i], foreground='blue')
                    self.locklist[i]=3
                else:self.rbutton[i].configure(text = '')
        def place(self, event=None): #puts number into block
            radio = self.rb.get()
            num = self.nb.get()
            if self.locklist[radio] == 0:
                self.inVarlist[radio] = num
                self.rbutton[radio].configure(text = num)
        def numbers(self, event): #changes number when pressed on keyboard and puts into block
            self.nb.set(int(event.char))
            radio = self.rb.get()
            num = self.nb.get()
            if self.locklist[radio] == 0:
                self.inVarlist[radio] = num
                self.rbutton[radio].configure(text = num)
        def upkey(self, event): #moves 1 block up when up arrow is pressed on keyboard
            r = self.rb.get()
            x = r%10 - 1
            r -= 9
            if r <=0:r=73+x
            self.rb.set(r)
        def downkey(self, event): #moves 1 block down when down arrow is pressed on keyboard
            r = self.rb.get()
            x = r%10 - 2
            r += 9
            if r >=81:r=0+x
            self.rb.set(r)
        def leftkey(self, event): #moves 1 block left when left arrow is pressed on keyboard
            r = self.rb.get()
            x = r%9
            r -= 1
            if x == 1: r+=9
            self.rb.set(r)
        def rightkey(self, event): #moves 1 block right when right arrow is pressed on keyboard
            r = self.rb.get()
            x = r%9
            r += 1
            if x == 0: r-=9
            self.rb.set(r)
        def lock(self): #lock/unlock block
            radio = self.rb.get()
            if self.locklist[radio] == 0: #locks block so it cannot be changed
                self.rbutton[radio].configure(foreground='blue')
                self.locklist[radio] = 1
            elif self.locklist[radio] == 1: #unlocks block so it can be changed
                self.rbutton[radio].configure(foreground='black')
                self.locklist[radio] = 0
        def delete(self):
            r = self.rb.get()
            if self.locklist[r]==0:
                self.inVarlist[r] = 0
                self.rbutton[r].configure(text = '')
    root = Tk()
    app  = App(root)
    root.mainloop()
    alright theres the whole thing.
  6. #4
  7. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    alright i changed it up abit. i wrote a different program that has my problem in it. here it is. click 'new game' then 'change list' and the 'new game' again. thats basically what it happening in my original program

    Code:
    from Tkinter import *
    a = [1,2,3,4]
    class App:
        def __init__(self, root):
            self.root = root
            Button(root, text='New Game', command=self.ng).grid(row=0, column=0)
            Button(root, text='Change List', command=self.cl).grid(row=1, column=0)
        def ng(self):
            self.mylist = a
            print self.mylist
        def cl(self):
            self.mylist[1] = 96
            print self.mylist
    root = Tk()
    app  = App(root)
    root.mainloop()
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    1
    Rep Power
    0
    Just in case you haven't figured this one out yet.

    #What you are doing
    >>> a = [1,2,3,4]
    >>> a
    [1, 2, 3, 4]
    >>> y = a
    >>> y
    [1, 2, 3, 4]
    >>> y[2] = 7
    >>> y
    [1, 2, 7, 4]
    >>> a
    [1, 2, 7, 4]
    >>>

    #What you want to do
    >>> a = [1,2,3,4]
    >>> a
    [1, 2, 3, 4]
    >>> y = a[:]
    >>> y
    [1, 2, 3, 4]
    >>> y[2] = 7
    >>> y
    [1, 2, 7, 4]
    >>> a
    [1, 2, 3, 4]
  10. #6
  11. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    yea i figured it out... its an assignment problem

IMN logo majestic logo threadwatch logo seochat tools logo