473,654 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusing: Canvas image working in function but not in class

I have an image that displays on a canvas that works unless I put the
same code in a class. I can't figure that out. Here's what works:

def uts5100(self):
self.screen = Toplevel( self.master )
self.screen.geo metry("+100+50" )
self.screen.gra b_set()
self.screen.foc us_set()

self.screenType = "uts5100"

self.canvas = Canvas(self.scr een)

self.canvas.cre ate_image(20, 20, image = self.empty5100,
anchor=NW, tags = "chasis" )

self.canvas.pac k()

The image is opened in another function:

def openImages(self ):
self.empty5100 = ImageTk.PhotoIm age( Image.open("Ima ges/" +
"5100empty.bmp" ) )
However if I do the same thing, but in it's own class, the image does
not appear:

def uts5100(self):
sys5100 = UTS5100( self.master )

class UTS5100:
def __init__(self, master):
self.master = master
self.openImages ()
self.draw()

def openImages(self ):
self.empty5100 = ImageTk.PhotoIm age( Image.open("Ima ges/" +
"5100empty.bmp" ) )
def draw(self):
self.screen = Toplevel( self.master )
self.screen.geo metry("+100+50" )
self.screen.gra b_set()
self.screen.foc us_set()

self.screenType = "uts5100"

self.canvas = Canvas(self.scr een)

self.image = self.canvas.cre ate_image(20, 20, image =
self.empty5100, anchor=NW, tags = "chasis" )

self.canvas.pac k()
They are called from a button that when pressed creates the window
where the image is supposed to appear. Obviously there is something
that I am missing when putting this window into it's own class, but I
can't figure it out.

Anyone got an idea why this is happening?

Thanks

Jul 19 '05 #1
1 1556
I will answer my own question in case anyone else ever has this
problem.

I knew the problem (although some say it's not) existed with the
namespace of pictures, that if you didn't save the pictures in
persistent memory they would disappear as soon as the function that
called them was exited. So a while ago I went through this and fixed it
by saving all my pictures and widgets that held them as class objects.

However, apparently I was causing the same thing to happen by not
saving the class instance as an object of the class that called it. So
this fixed it:

def uts5100(self):
self.sys5100 = UTS5100( self.master )

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4482
by: Tuvas | last post by:
I'm trying to display a picture on a Tkinter Canvas. It seems to work fine the first time that it is displayed. However, subsequent times running shows an error like this: TCLerror: Wrong # args: should be ".-1211472948 .-1211470996 addtag tag searchCommand ?arg arg ...? My code works like this: if (pic):
1
15897
by: namratapatil | last post by:
Hi...i am trying to load an image that has been saved at the following path: C:\WTK22\apps\MyProject\res After exectuing the program, the NullPointerException is thrown...Kindly let me know how to load the image without any exceptions being thrown... import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class ImmutableImageExample extends MIDlet { private Display display; private MyCanvas canvas;
11
2077
by: Aaron Gray | last post by:
Hi, I have put together a bit of JavaScript to make a square resizable canvas :- http://www.aarongray.org/Test/JavaScript/resizable.html Problems I have :- a) I cannot seem to center it horrizontally b) It does not appear to be totaly square on either of my machines.
2
18283
TMS
by: TMS | last post by:
Schools over!!! Now its time to play. I would like to learn how to make objects move from one location to the next on a canvas widget. For example: from Tkinter import * class square: def __init__(self, canvas, xy, color, change): self.canvas = canvas self.id = self.canvas.create_rectangle(-10-abs(change),
26
3546
by: Jon Davis | last post by:
OK, why is Canvas not IDisposable, and how do I get rid of all the Windows handles? I'm doing a performance test of looping through a dynamic XAML-to-JPEG conversion. It gets to about 500 conversions and then crashes. Task Manager says that about 6000 Windows handles were created and the count never decrements. My code (loop not shown):
4
3947
by: foleyflint | last post by:
Hello, lately I've been trying to make an applet that has a background image and a couple of canvas objects on it holding an image each. I created the class ImageCanvas which extends Canvas adn draws a picture in the canvas. Next I have to place the canvas on the applet and there I got stuck because I don't know how to control the position of every canvas. How can I put a canvas exactly there where I want it to be? Here is my code so far:...
6
3543
by: Nebulism | last post by:
I have been attempting to utilize a draw command script that loads a canvas, and through certain mouse events, draws rectangles. The original code is from http://www.java2s.com/Code/Python/Event/Usemousetodrawashapeoncanvas.htm . The code itself is: from Tkinter import * trace = 0 class CanvasEventsDemo: def __init__(self, parent=None): canvas = Canvas(width=300, height=300, bg='beige') canvas.pack()
4
9132
by: skanemupp | last post by:
mapq = PhotoImage(file = 'C:\Users\saftarn\Desktop\elmapovic.gif') w.create_image(10, 10, image = mapq, anchor = NW) after doing this is there any possibility of getting the characteristics of the GIF-picture(or bitmap if i use that)? it would be very helpfull if i for example could do something like canvas.getcolor(image, mouseclick.x,mouseclick.y) if u get the point. get the color of the image where i clicked.
3
7466
by: skanemupp | last post by:
so i load a gif onto a canvas and when i click the canvs i want to get the color of the pixel that is clicked. so i need to ge the object im clicking. i was told in another thread to use find_withtag or find_closest but it is not working, maybe im using the method on the wrong object. how do i do this? and how do i then get specifics about that object, ie the pixel-color? Exception in Tkinter callback
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.