By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

tkinter blues (greens, reds, ...)

P: n/a
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

Oct 28 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Sean McIlroy wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 28 '05 #2

P: n/a


Steve Holden wrote:
Sean McIlroy wrote:
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

Oct 28 '05 #3

P: n/a
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.fd l,frontback)

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

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

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

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

def U2_(): rubik.cycle(rubik.ful,rubik.fur,rubik.bur,rubik.bu l,updown)
def D1_(): rubik.cycle(rubik.bdl,rubik.bdr,rubik.fdr,rubik.fd l,updown)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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(s ide=TOP)

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

Button(left,text='v\nv',width=3,command=L2).pack(s ide=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(si de=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=LE FT)

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

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

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

front = Frame(root,borderwidth=3)

front.pack(side=TOP)

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

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

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

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

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()

Oct 28 '05 #4

P: n/a


Sean McIlroy wrote:
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
Oct 29 '05 #5

P: n/a
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

Oct 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.