Time issue using Tkinter module

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

Time issue using Tkinter module

Postby Johnnybilliboy » Fri Aug 23, 2013 11:59 am

Hello everyone,

The aim of my code is to control pixels (number, position, color) on a background which is also controlled in terms of color and size. The code is composed of two files :

- the module Mapping.py with all the needed functions :

Code: Select all
import Tkinter as tk

class Mapp:
   
    def size_canvas(self,size_canvas):
        root = tk.Tk()
        self.canvas = tk.Canvas(root, width = size_canvas[0], height = size_canvas[1])
        self.canvas.pack()

    def background(self,size,color):
        w,h = size
        r,g,b = color
        self.canvas.create_rectangle(0,0,w,h,fill="#%02x%02x%02x" % (r,g,b), outline = "#%02x%02x%02x" % (r,g,b))

   
    def rectangle(self,pos,size,color,color_outline):
        x,y = pos
        dx,dy = size
        r,g,b = color
        r_outline,g_outline,b_outline = color_outline
        self.canvas.create_rectangle(x-1,y-1,x+dx,y+dy,fill="#%02x%02x%02x" % (r,g,b), outline = "#%02x%02x%02x" % (r_outline,g_outline,b_outline))

    def pixel(self,pos,color,color_outline):
        self.rectangle(pos,(1,1),color,color_outline)


- the main.py :

Code: Select all
#----== Modules perso ==----
import Mapping

#----== Modules python ==----
import time


#----== Constants ==----
color_background = (255,255,255)
size_background = (200,200)
color_object = (0,0,0)
size_object = (5,5)
pos_object_1 = (10,10)
pos_object_2 = (20,20)

#----== Canvas ==----
m=Mapping.Mapp()
m.size_canvas((200,200))

#----== Background ==----
m.background(size_background,color_background)

#----== Pixels ==----
p1 = m.pixel(pos_object_1,color_object,color_background)
p2 = m.pixel(pos_object_2,color_object,color_background)

Mapping.tk.mainloop()



This peace of code works fine : white window appears and two black pixels are displayed on it (p1 and p2 at position pos_object_1 and pos_object_1 respectively).

What I want to do :

I want to display pixel p1 and p2 with waiting period between them (5 sec for example).

I also tried after method from Canvas between p1 and p2 but it doesn't work. I also tried sleep method but the waiting period i before that the window is displayed no matter where the method is inserted on the code.

If some body could help me.

Thanks.

PS : Sorry for my English which is probably not so academic but i am French.
Johnnybilliboy
 
Posts: 2
Joined: Fri Aug 23, 2013 9:35 am

Re: Time issue using Tkinter module

Postby wuf » Fri Aug 23, 2013 10:15 pm

Hi Johnnybilliboy

Here something to play around:
Code: Select all
try:
    #~~ For Python 2.x
    import Tkinter as tk
except ImportError:
    #~~ For Python 3.x
    import tkinter as tk

class Mapp(tk.Canvas):
   
    def __init__(self, parent, size, time_step=1000, **options):
        self.parent = parent
        self.time_step = time_step
        self.mapp_width, self.mapp_height = size
        self.pos_objects = list()
       
        tk.Canvas.__init__(self, parent,
            width=self.mapp_width, height=self.mapp_height, **options)
       
        self.sampler()
       
    def background(self, color):
        self.item_config(bg=color)

    def rectangle(self, geometry, **options):
        xpos, ypos, width, height = geometry
        coords = (xpos, ypos, xpos+width, ypos+height)
        self.create_rectangle(coords, **options)
   
    def show_objects_instantly(self, pos_objects):
        for pos_object in pos_objects:
            self.place_pixel(pos_object)
   
    def show_objects_delayed(self, pos_objects):
        for pos_object in pos_objects:
            self.pos_objects.append(pos_object)
           
    def place_pixel(self, pos_object):
        pos, color = pos_object
        xpos, ypos = pos
        coords = (xpos, ypos, xpos+1, ypos+1)
        self.create_rectangle(coords,fill=color, width=0)
   
    def sampler(self):
        if self.pos_objects != []:
            pos_object =  self.pos_objects[0]
            del self.pos_objects[0]
            self.place_pixel(pos_object)           
           
        self.after(self.time_step, self.sampler)
       
APP_WIN_XPOS = 50
APP_WIN_YPOS = 50

MAPP_BACKGROUND = 'steelblue'
MAPP_SIZE = (200,200)
MAPP_TIME_STEP = 1000
OBJECT_COLOR = 'yellow'
OUTLINE_COLOR = MAPP_BACKGROUND
         
class App(object):
   
    def __init__(self):
        self.win = tk.Tk()
        self.win.geometry('+{0}+{1}'.format(APP_WIN_XPOS, APP_WIN_YPOS))
        self.win.protocol("WM_DELETE_WINDOW", self.close)
        self.win.title("Pixel-Mapping")

        self.mapp = Mapp(self.win,
            MAPP_SIZE, time_step=MAPP_TIME_STEP, bg=MAPP_BACKGROUND,
            highlightthickness=0)
        self.mapp.pack()

        self.mapp.rectangle((150,10,40,40), fill='green', outline='red',
            width=0)
       
        # Create 10 pos_objects
        sample_pos_objects = [
            ((point, point), OBJECT_COLOR) for point in range(10, 100, 10)]
        # Single pos_object
        pos_object_1 = [((10, 40), 'white')]
       
#        self.mapp.show_objects_instantly(sample_pos_objects)
#        self.mapp.show_objects_instantly(pos_object_1)
       
        self.mapp.show_objects_delayed(sample_pos_objects)
        self.mapp.show_objects_delayed(pos_object_1)
       
    def run(self):
        self.win.mainloop()
   
    def close(self):
        print('Do something before shutdown')
        self.win.destroy()
           
app = App().run()

wuf ;)
wuf
 
Posts: 38
Joined: Fri Feb 08, 2013 6:42 am

Re: Time issue using Tkinter module

Postby Johnnybilliboy » Mon Aug 26, 2013 5:59 am

You make my Monday start wonderfully !! Thank you so much Wut !
Johnnybilliboy
 
Posts: 2
Joined: Fri Aug 23, 2013 9:35 am


Return to GUI

Who is online

Users browsing this forum: Bing [Bot] and 2 guests