0

I'm trying to learn tkinter by writing virtual camera. Window with canvas is popping up, handlers are reading pressed keys and calling functions. But when I'm pressing 'P' button associated with clearing canvas, writing message and closing window nothing except closing windows really happens. And I honestly don't know what am I doing wrong. Could you help me finding out why I can't clear elements and write here?

import numpy
import sys
import tkinter
from time import sleep
import math

end = False
STEP = 5
ZOOM = 20
ROTATION = math.pi/15

"""
def zoom(symbol):

def move(vector):

def rotate(direction):

"""
def fin():
    global end
    end = True

def key(event):
    handler = {
        'w': lambda: move([0, 0, -STEP]),
        's': lambda: move([0, 0, STEP]),
        'a': lambda: move([STEP, 0, 0]),
        'd': lambda: move([-STEP, 0, 0]),
        'q': lambda: move([0, -STEP, 0]),
        'e': lambda: move([0, STEP, 0]),
        'r': lambda: zoom(True),
        'f': lambda: zoom(False),
        'y': lambda: rotate(8),
        'h': lambda: rotate(2),
        'g': lambda: rotate(4),
        'j': lambda: rotate(6),
        't': lambda: rotate(1),
        'u': lambda: rotate(3),
        'p': lambda: fin(),
    }.get(event.char)

    if handler:
        handler()
        render()

def render():
    if end:
        canvas.delete("all")
        canvas.create_text(200, 30, fill="white", text="bye bye")
        canvas.pack()
        sleep(2)
        sys.exit('elo')
    canvas.pack()

root = tkinter.Tk()
root.title('Camera')
canvas = tkinter.Canvas(root, width=1024, height=768, bg='black')
canvas.create_line(0, 100, 200, 0, fill="red")
root.bind('<Key>', key)
render()
tkinter.mainloop()
Piesa
  • 21
  • 3

1 Answers1

0

First of all I would suggest that you take a look at how pack is used. You only need to call pack once.

Additionally, using sleep will not work for your purpose since tkinter will not update the window until either the end or the start of the next update loop.

I would suggest you try using after instead. You can find a helpful thread here.

Edit:

To answer your comment. You need to provide a callback for the after function. It is this function that is called after 5000 ms. Rather than using sys.exit, what you can do is use destroy; which is a bit cleaner. Modify your code like so

def render():
    if end:
        canvas.delete("all")
        canvas.create_text(200, 30, fill="white", text="bye bye")
        root.after(5000, root.destroy)

and it should work.

  • Oh, thanks. I've read about pack() and I guess I managed to correct my code in terms of this, but i still have problem with after. I have used it the same way as in link you posted, but it didn't worked for me. Then I read in docimentation that I can omit the callback, so I just ended with [code modified this way](https://pastebin.com/JirkfSdN) But i still only manage to have delay. First line is not deleted, and text don't show up. But if i skip the after() function and sys.exit I can see, that there is text instead of line. Am I just calling this function wrong way? – Piesa Oct 28 '18 at 16:04