Drawing on tkinter canvas from command line
<p>I read some tutorial (basic) on tkinter and learned how to create a mainloop and add gui elements to that. Also learned how to bind actions to button widgets.</p>

<p>Now I would like to do this: </p>

<ol>
<li>launch the tkinter canvas</li>
<li>be able to read command from the console and update the canvas after those commands.</li>
</ol>

<p>example:
I write command with arguments on the console and some graphics elements is being added to the canvas (and canvas is updated after).</p>

<p>Is something possible, maybe threding related?
Can you point me in one direction which you think is the most reasonable to follow?</p>

<p>Came up with this:</p>

<pre><code>from Tkinter import *
import random

root = Tk()
width = 800
height = 600

def key(event):
    s = raw_input("CMD: ")

    if s == 'quit':
        root.destroy()

    if s == 'l':
        x1 = random.randint(0,width)
        x2 = random.randint(0,width)
        y1 = random.randint(0,height)
        y2 = random.randint(0,height)
        frame.create_line(x1,y1,x2,y2)
        frame.focus_force()



frame = Canvas(root, width=width, height=height)
frame.bind("&lt;Key&gt;", key)
frame.pack()
frame.focus_set()

root.mainloop()
</code></pre>

<p>In this way it is a bit complicated because before entering something on the console I have to get the focus back clicking on its window. Maybe it would be nicer to read command from Tkinter directly and then opening dialogs for setting up command parameters.</p>

python canvas tkinter