tkinter blues (greens, reds, ...)

S

Sean McIlroy

hi all

i recently wrote a script that implements a puzzle. the interface
mostly consists of a bunch of colored disks on a tkinter canvas. the
problem is that the disks change their colors in ways other than the
way they're supposed to. it certainly isn't just a bug in my script,
since i can sometimes change the color of certain disks just by taking
focus off of the window and then bringing it back again! does this
sound like some known bug in tkinter? and if so, is there a recommended
way of working around it? if it matters, i'm using python 2.3 under
windows 95. any advice will be much appreciated.

peace
 
S

Steve Holden

Sean said:
hi all

i recently wrote a script that implements a puzzle. the interface
mostly consists of a bunch of colored disks on a tkinter canvas. the
problem is that the disks change their colors in ways other than the
way they're supposed to. it certainly isn't just a bug in my script,
since i can sometimes change the color of certain disks just by taking
focus off of the window and then bringing it back again! does this
sound like some known bug in tkinter? and if so, is there a recommended
way of working around it? if it matters, i'm using python 2.3 under
windows 95. any advice will be much appreciated.
It sounds to me much more like a bug in your script, to me at least.
Change of focus generates windowing events in much the same way as
clicking a button or hitting a key does, so I don't understand why you
think that "just [by] taking the focus off the window and bringing it
back again" shouldn't change anything.

For more specific insights we'd need to see some code, but sometimes
just changing your own focus from "Tkinter has a bug" to "my code has a
bug" is enough to help one find out what the problem really is. If you
have a soft toy I'd recommend you sit it down somewhere and explain to
it in great detail exactly why it can't be a bug in your program. You
may find you discover the error with no further assistance.

If not, fire the toy and ask again :)

regards
Steve
 
R

Ron Adam

Steve said:
Sean said:
hi all

i recently wrote a script that implements a puzzle. the interface
mostly consists of a bunch of colored disks on a tkinter canvas. the
problem is that the disks change their colors in ways other than the
way they're supposed to. it certainly isn't just a bug in my script,
since i can sometimes change the color of certain disks just by taking
focus off of the window and then bringing it back again! does this
sound like some known bug in tkinter? and if so, is there a recommended
way of working around it? if it matters, i'm using python 2.3 under
windows 95. any advice will be much appreciated.
It sounds to me much more like a bug in your script, to me at least.
Change of focus generates windowing events in much the same way as
clicking a button or hitting a key does, so I don't understand why you
think that "just [by] taking the focus off the window and bringing it
back again" shouldn't change anything.

For more specific insights we'd need to see some code, but sometimes
just changing your own focus from "Tkinter has a bug" to "my code has a
bug" is enough to help one find out what the problem really is. If you
have a soft toy I'd recommend you sit it down somewhere and explain to
it in great detail exactly why it can't be a bug in your program. You
may find you discover the error with no further assistance.

If not, fire the toy and ask again :)

regards
Steve

To add to Steve's humorous perosonificatious techniques. You should
probably check that you aren't inadvertently using some sort of object
id or window handle as a color value. As long as the object you use
returns an integer you won't get an error message, but instead get
different colors when the canvas object is updated. Like when changing
the focus.

Another place to look is where you may be adding or converting rgb color
values.

This function convert decimal rgb values to a hex rgb string that
tkinter expects.

def rgb(red, green, blue):
""" Convert RGB value of 0 to 255 to
hex Tkinter color string.
"""
return '#%02x%02x%02x' % (red, green, blue)

Cheers,
Ron
 
S

Sean McIlroy

i'm using the canned colors ("pink", "orange", etc). should i try
changing to explicit color specifications to see if that makes a
difference? i'm not sure what the other guy meant by a "soft toy", but
i take it the idea is to try and construct a correctness proof for the
script, and see what keeps it (the proof) from working. it's a sound
idea, of course, but the script is pretty darn straightforward, as you
can see (below). anyway, i'll let you know how it turns out.

peace

############################


green = 'green'

orange = 'orange'

pink = 'pink'

yellow = 'yellow'

red = 'red'

blue = 'blue'



fb = 'frontback'

ud = 'updown'

lr = 'leftright'



def select(x,a,b): return b[a.index(x)]

def frontback(orientation): return
select(orientation,[fb,ud,lr],[fb,lr,ud])

def updown(orientation): return
select(orientation,[fb,ud,lr],[lr,ud,fb])

def leftright(orientation): return
select(orientation,[fb,ud,lr],[ud,fb,lr])



class cell:

def __init__(self, circlecolor, pointercolor, orientation,
coordinates):

self.circlecolor = circlecolor

self.pointercolor = pointercolor

self.orientation = orientation

self.coordinates = coordinates

def endpoint(self):

a,b = self.coordinates

if self.orientation==fb:

if abs(a)==1: return [2*a,2*b]

if abs(a)==2: return [a/2,b/2]

if self.orientation==ud: return [+a,-b]

if self.orientation==lr: return [-a,+b]



class cube:

def __init__(self):

self.ful = cell(green, blue, fb, [-2,+2])

self.fur = cell(orange, blue, fb, [+2,+2])

self.fdl = cell(pink, blue, fb, [-2,-2])

self.fdr = cell(yellow, blue, fb, [+2,-2])

self.bul = cell(green, red, fb, [-1,+1])

self.bur = cell(orange, red, fb, [+1,+1])

self.bdl = cell(pink, red, fb, [-1,-1])

self.bdr = cell(yellow, red, fb, [+1,-1])

self.cells =
[self.ful,self.fur,self.fdl,self.fdr,self.bul,self.bur,self.bdl,self.bdr]

def redraw(self,*cells):

for x in cells:

A = x.coordinates

B = x.endpoint()

erase(*A)

drawpointer(color=x.pointercolor,*(A+B))

drawcircle(color=x.circlecolor,*A)

def display(self):

self.redraw(*self.cells)

def cycle(self,a,b,c,d,funct):

x = d.circlecolor

y = d.pointercolor

z = funct(d.orientation)

d.circlecolor = c.circlecolor

d.pointercolor = c.pointercolor

d.orientation = funct(c.orientation)

c.circlecolor = b.circlecolor

c.pointercolor = b.pointercolor

c.orientation = funct(b.orientation)

b.circlecolor = a.circlecolor

b.pointercolor = a.pointercolor

b.orientation = funct(a.orientation)

a.circlecolor = x

a.pointercolor = y

a.orientation = z





rubik = cube()



def F1_():
rubik.cycle(rubik.ful,rubik.fur,rubik.fdr,rubik.fdl,frontback)

def F2_():
rubik.cycle(rubik.fdl,rubik.fdr,rubik.fur,rubik.ful,frontback)

def B1_():
rubik.cycle(rubik.bdl,rubik.bdr,rubik.bur,rubik.bul,frontback)

def B2_():
rubik.cycle(rubik.bul,rubik.bur,rubik.bdr,rubik.bdl,frontback)



def U1_(): rubik.cycle(rubik.bul,rubik.bur,rubik.fur,rubik.ful,updown)

def U2_(): rubik.cycle(rubik.ful,rubik.fur,rubik.bur,rubik.bul,updown)


def D1_(): rubik.cycle(rubik.bdl,rubik.bdr,rubik.fdr,rubik.fdl,updown)

def D2_(): rubik.cycle(rubik.fdl,rubik.fdr,rubik.bdr,rubik.bdl,updown)



def L1_():
rubik.cycle(rubik.ful,rubik.bul,rubik.bdl,rubik.fdl,leftright)

def L2_():
rubik.cycle(rubik.fdl,rubik.bdl,rubik.bul,rubik.ful,leftright)

def R1_():
rubik.cycle(rubik.fur,rubik.bur,rubik.bdr,rubik.fdr,leftright)

def R2_():
rubik.cycle(rubik.fdr,rubik.bdr,rubik.bur,rubik.fur,leftright)



def F1(): F1_(); rubik.redraw(rubik.ful,rubik.fur,rubik.fdr,rubik.fdl)

def F2(): F2_(); rubik.redraw(rubik.fdl,rubik.fdr,rubik.fur,rubik.ful)

def B1(): B1_(); rubik.redraw(rubik.bdl,rubik.bdr,rubik.bur,rubik.bul)

def B2(): B2_(); rubik.redraw(rubik.bul,rubik.bur,rubik.bdr,rubik.bdl)

def U1(): U1_(); rubik.redraw(rubik.bul,rubik.bur,rubik.fur,rubik.ful)

def U2(): U2_(); rubik.redraw(rubik.ful,rubik.fur,rubik.bur,rubik.bul)

def D1(): D1_(); rubik.redraw(rubik.bdl,rubik.bdr,rubik.fdr,rubik.fdl)

def D2(): D2_(); rubik.redraw(rubik.fdl,rubik.fdr,rubik.bdr,rubik.bdl)

def L1(): L1_(); rubik.redraw(rubik.ful,rubik.bul,rubik.bdl,rubik.fdl)

def L2(): L2_(); rubik.redraw(rubik.fdl,rubik.bdl,rubik.bul,rubik.ful)

def R1(): R1_(); rubik.redraw(rubik.fur,rubik.bur,rubik.bdr,rubik.fdr)

def R2(): R2_(); rubik.redraw(rubik.fdr,rubik.bdr,rubik.bur,rubik.fur)



def solve():

rubik.__init__()

rubik.display()



def scramble():

n = 15

from random import randint

f = [F1_,F2_,B1_,B2_,U1_,U2_,D1_,D2_,L1_,L2_,R1_,R2_]

for i in range(n): f[randint(0,11)]()

rubik.display()



canvaswidth = 380

canvasheight = 330



def coordinates(x,y): return (canvaswidth/2+68*x),(canvasheight/2-68*y)



def drawcircle(x,y,color):

r = (20,15)[abs(x)==1]

a,b = coordinates(x,y)

canvas.create_oval(a-r,b-r,a+r,b+r,fill=color)



def drawpointer(x1,y1,x2,y2,color):

a1,b1 = coordinates(x1,y1)

a2,b2 = coordinates(float(x1+x2)/2,float(y1+y2)/2)

canvas.create_line(a1,b1,a2,b2,width=2,fill=color,arrow=LAST)



def erase(x,y):

a,b = coordinates(x,y)

p = canvas.find_overlapping(a-10,b-10,a+10,b+10)

q = [x for x in p if canvas.type(x) in ('oval','line')]

for x in q: canvas.delete(x)



def drawbackground():

A = coordinates(+1,+1)

B = coordinates(+1,-1)

C = coordinates(-1,+1)

D = coordinates(-1,-1)

E = coordinates(+2,+2)

F = coordinates(+2,-2)

G = coordinates(-2,+2)

H = coordinates(-2,-2)

canvas.create_polygon(fill='',outline='black',*(C+A+E+G+C))

canvas.create_polygon(fill='',outline='black',*(C+G+H+D+C))

canvas.create_polygon(fill='',outline='black',*(A+E+F+B+A))

canvas.create_polygon(fill='',outline='black',*(H+F+B+D+H))



from Tkinter import *

root = Tk()

########################################################

back = Frame(root,borderwidth=3)

back.pack(side=TOP)

Button(back,text='<<<<',command=B1).pack(side=LEFT)

Label(back,width=10,text='BACK').pack(side=LEFT)

Button(back,text='>>>>',command=B2).pack(side=LEFT)

########################################################

top = Frame(root,borderwidth=3)

top.pack(side=TOP)

Button(top,text='<<<<',command=U1).pack(side=LEFT)

Label(top,width=10,text='TOP').pack(side=LEFT)

Button(top,text='>>>>',command=U2).pack(side=LEFT)

########################################################

mid = Frame(root)

mid.pack(side=TOP)

left = Frame(mid,borderwidth=5)

left.pack(side=LEFT)

Button(left,text='^\n^',width=3,command=L1).pack(side=TOP)

Label(left,height=6,text='L\nE\nF\nT').pack(side=TOP)

Button(left,text='v\nv',width=3,command=L2).pack(side=TOP)

canvas = Canvas(mid,width=canvaswidth,height=canvasheight)

canvas.pack(side=LEFT,expand=1,fill=BOTH)

right = Frame(mid,borderwidth=5)

right.pack(side=LEFT)

Button(right,text='^\n^',width=3,command=R1).pack(side=TOP)

Label(right,height=6,text='R\nI\nG\nH\nT').pack(side=TOP)

Button(right,text='v\nv',width=3,command=R2).pack(side=TOP)

########################################################

bottom = Frame(root,borderwidth=3)

bottom.pack(side=TOP)

Button(bottom,text='<<<<',command=D1).pack(side=LEFT)

Label(bottom,width=10,text='BOTTOM').pack(side=LEFT)

Button(bottom,text='>>>>',command=D2).pack(side=LEFT)

########################################################

front = Frame(root,borderwidth=3)

front.pack(side=TOP)

Button(front,text='<<<<',command=F1).pack(side=LEFT)

Label(front,width=10,text='FRONT').pack(side=LEFT)

Button(front,text='>>>>',command=F2).pack(side=LEFT)

########################################################

ctrl = Frame(root,borderwidth=20)

ctrl.pack(side=TOP)

Button(ctrl,text='SOLVE',command=solve).pack(side=LEFT)

Label(ctrl,width=3,text='').pack(side=LEFT)

Button(ctrl,text='SCRAMBLE',command=scramble).pack(side=LEFT)

########################################################

root.title('rubik')

drawbackground()

rubik.display()

root.mainloop()
 
R

Ron Adam

Sean said:
i'm using the canned colors ("pink", "orange", etc). should i try
changing to explicit color specifications to see if that makes a
difference? i'm not sure what the other guy meant by a "soft toy", but
i take it the idea is to try and construct a correctness proof for the
script, and see what keeps it (the proof) from working. it's a sound
idea, of course, but the script is pretty darn straightforward, as you
can see (below). anyway, i'll let you know how it turns out.

peace

Hmm... It worked fine for me. I'm using python 2.4.1 on windows XP.

I didn't see anything particularly wrong with the program that might
cause the problem you are referring to. So I'm afraid I can't help much.

Maybe someone with 2.3 can reproduce it?

BTW, Nice puzzle, much harder than it looks.

Cheers,
Ron
 
S

Sean McIlroy

hi ron

changing from english words to hexadecimal numerals did the trick for
me, so everything's cool now. thanks for looking at it.

peace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,025
Latest member
Rigor4

Latest Threads

Top