473,785 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter: how to refresh a canvas without duplicates?

I've got a Tkinter app that draws three histograms. At this point I am
simulating real data by drawing rectangles whose size is defined by random
numbers; in the future there would be real data coming from a server.

I want to redraw the rectangles every time I press the Again button. This works
for me, but I get an extra set of rectangles each time I press the Again button.
How to get rid of this effect?

Here's the app so you can see the behaviour:

Thanks
S

---
title = 'Histogram summary'

# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']

from Tkinter import *
import Pmw
import random

global canwidth, canheight, sectors, barcolors
sectors = 'SECT_1','SECT_ 2','SECT_3'
barcolors = 'green','yellow ','red','violet '
canwidth = 200
canheight = 20
class Summary:
def __init__(self, parent):
# Create the dialog.
self.dialog = Pmw.Dialog(pare nt,
buttons = ('OK', 'Apply', 'Cancel', 'Help'),
defaultbutton = 'OK',
title = 'My dialog',
command = self.execute)
self.dialog.wit hdraw()

# Add some contents to the dialog.
w = Label(self.dial og.interior(),
text = 'Sector Summary',
background = 'black',
foreground = 'white',
pady = 20)
w.pack(expand = 1, fill = 'both', padx = 2, pady = 2)
def drawRec(self):
global row,can
for sector in sectors:
row = Frame(self.dial og.interior())
lab = Label(row, width=10,text=s ector)
can = Canvas(row,
width=canwidth,
height=canheigh t
)

h=canheight
m=canwidth-20 # alarm level (100%) is less than available space
for rectangle d=canwidth/2
p=random.randra nge(-1,2,2) # create random + or - imbalance
f=random.randra nge(-110,110,1)/100.0 # create random fractional
height of bar w=f*m
startx=d+f*d
starty=0
endx=d
endy=h
if (f<-1.0):
rcolor='violet'
elif (-1.0<=f<0.85):
rcolor='green'
elif (0.85<f<=1.0):
rcolor='yellow'
elif (f>1.0):
rcolor='red'
else:
rcolor='grey'

can.create_rect angle(startx,st arty,endx,endy, width=1,fill=rc olor)
row.pack(side=T OP, fill=X) lab.pack(side=L EFT)
can.pack(side=L EFT, expand=0, padx = 1, pady = 1) #grow
horizontal self.dialog.act ivate(globalMod e = 'nograb')

def redrawRec(self) :
self.dialog.dea ctivate()
for sector in sectors:
row.destroy()
self.drawRec()
self.dialog.act ivate()

def execute(self, result):
print 'You clicked on', result
if result not in ('Apply', 'Help'):
self.dialog.dea ctivate(result)
if (result =='Apply'):
self.redrawRec( )

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

# Create demo in root window for testing.
if __name__ == '__main__':
root = Tk()
#Pmw.initialise (root)
#root.title(tit le)

widget = Summary(root)
goButton = Button(root, text = 'Again', command = lambda: widget.drawRec( ))
goButton.pack(s ide = 'bottom')
exitButton = Button(root, text = 'Exit', command = root.destroy)
exitButton.pack (side = 'bottom')
widget.drawRec( )
root.mainloop()
---
Jul 18 '05 #1
0 2135

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

Similar topics

2
4017
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the extra step is necessary. This causes the "graph" function to execute when the button is created: Button(root, text='OK', command=graph(canvas))) However, this waits until the button is pressed (the desired behavior): def doit(): graph(canvas)
1
10730
by: Elaine Jackson | last post by:
Newbie. Playing with the 'turtle' module and wondering if there's a way to save the graphics you make with it. The documentation itself has nothing to say about this, nor (as far as I can tell) does the FAQ list at python-dot-org. Any pointers would be very much appreciated. (In related news, I would also like to find out what's going on with "bounding boxes" in image files - not having them is keeping me from including images in latex...
4
3131
by: pavel.kosina | last post by:
It seems to me that in my "again and again repainting canvas" script the rendering is slowing down as the time goes. It is visible even after 10 seconds. Any idea why? -- geon The exception is rule.
1
2977
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 scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
0
3587
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 accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
2
3737
by: Tuvas | last post by:
I've been trying to use a canvas to display different pictures on a Tkinter interface. However, it doesn't seem to update the information. Ei, I have something like this. canvas=Canvas(master,blah...) canvas.pack() def change_pic(path): global pic image=Image() #I'm using PIL to use the images, but I
5
14997
by: Dean Allen Provins | last post by:
I need to determine the size of a canvas while the process is running. Does anyone know of a technique that will let me do that? Thanks, Dean
4
2886
by: Davy | last post by:
Hi all, I have written a simple Tkinter program, that is draw a rectangle in a canvas, when I press Up key, the rectangle move up. But the program seems work not properly? My environment is Python2.5+PythonWin. ##---------------------- from Tkinter import * class MyApp:
3
2983
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
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...
1
7502
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
6741
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
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.