checking relief in canvas

Post here if you need help with creating a Graphical User Interface in Python.

checking relief in canvas

Postby eslavko » Fri Dec 27, 2013 11:38 am

Hello...

I have list of canvas. In each canvas is some tool drawn. When I click tool I change attribute releif=SUNKEN, and all other to releif=GROOVE.
Now I need to check wich tool (canvas) has relief=SUNKEN?


Code: Select all
from Tkinter import *
root = Tk()
tools=[]
tools.append(Canvas(root,bd=2, height=40, width=40, relief=GROOVE).pack())
tools.append(Canvas(root,bd=2, height=40, width=40, relief=GROOVE).pack())
tools.append(Canvas(root,bd=2, height=40, width=40, relief=SUNKEN).pack())
tools.append(Canvas(root,bd=2, height=40, width=40, relief=GROOVE).pack())

#how to check wich tool has releif=SUNKEN?

root.mainloop()


eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: checking relief in canvas

Postby Somelauw » Fri Dec 27, 2013 1:31 pm

eslavko wrote:
Code: Select all
tools.append(Canvas(root,bd=2, height=40, width=40, relief=GROOVE).pack())


pack() returns None, so you are now creating a list which only contains None.

Try this instead:
Code: Select all
tools=[]
for i in range(4):
    tool = Canvas(root,bd=2, height=40, width=40, relief=GROOVE)
    tool.pack()
    tools.append(tool)

tools[2].relief = SUNKEN
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 68
Joined: Tue Feb 12, 2013 8:30 pm

Re: checking relief in canvas

Postby eslavko » Fri Dec 27, 2013 3:52 pm

Ok I make mistake when I stripped my code.
But the question is how to check relief in canvas. For now I have global variable to keep track wich canvas is last clicked. But I prefer to check in canvas list wich one has releif=SUNKEN.
So again. Question is how to check not how to set relief!
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: checking relief in canvas

Postby Yoriz » Fri Dec 27, 2013 4:26 pm

Dont pollute the namespace by using from Tkinter import * just use import Tkinter

call the method cget('relief') to get the relief value.
Code: Select all
import Tkinter
root = Tkinter.Tk()
tools = []
tools.append(Tkinter.Canvas(root, bd=2, height=40, width=40,
                            relief=Tkinter.GROOVE))
tools.append(Tkinter.Canvas(root, bd=2, height=40, width=40,
                            relief=Tkinter.GROOVE))
tools.append(Tkinter.Canvas(root, bd=2, height=40, width=40,
                            relief=Tkinter.SUNKEN))
tools.append(Tkinter.Canvas(root, bd=2, height=40, width=40,
                            relief=Tkinter.GROOVE))

# how to check wich tool has releif=SUNKEN?

for tool in tools:
    tool.pack()
    print tool.cget('relief')

root.mainloop()

Output
Code: Select all
groove
groove
sunken
groove
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 721
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: checking relief in canvas

Postby Somelauw » Fri Dec 27, 2013 6:18 pm

Yoriz wrote:call the method cget('relief') to get the relief value.


print(tool.relief) works for me as well.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 68
Joined: Tue Feb 12, 2013 8:30 pm

Re: checking relief in canvas

Postby Yoriz » Fri Dec 27, 2013 6:31 pm

Somelauw wrote:
Yoriz wrote:call the method cget('relief') to get the relief value.


print(tool.relief) works for me as well.


I'm guessing thats if your using python3.
python2.7 gives the following
Code: Select all
Traceback (most recent call last):
  File "**", line 19, in <module>
    print tool.relief
AttributeError: Canvas instance has no attribute 'relief'
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 721
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: checking relief in canvas

Postby eslavko » Fri Dec 27, 2013 6:38 pm

yes...

cget is what I need.

Thanks
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: checking relief in canvas

Postby eslavko » Fri Dec 27, 2013 9:10 pm

works with python 2.7!
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests