473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

tkinter blues (greens, reds, ...)

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
5 2155
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


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
1
by: Frank | last post by:
Hi, we are using oracle clients (Release 9.0.1.0.1 - Production) on an NT4 (Service Pack6) computers. the server is a W2K, (Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production With the...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
3
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it...
1
by: yvesd | last post by:
hello i want to intercept tkinter python system events like wm_delete_window and if possible for any window, but the newest code I've produced give me an error : Traceback (most recent call...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.