<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<link rel="alternate" type="text/html" href="https://gamma.unpythonic.net/"/>

<title>Jeff Epler's blog</title>
<modified>2024-10-06T14:15:01Z</modified>
<tagline>Photos, electronics, cnc, and more</tagline>
<author><name>Jeff Epler</name><email>jepler@unpythonic.net</email></author>
<entry>
<title>Talking directly to in-process tcl/tk</title>
<issued>2024-10-06T14:15:01Z</issued>
<modified>2024-10-06T14:15:01Z</modified>
<id>https://gamma.unpythonic.net/01728224101</id>
<link rel="alternate" type="text/html" href="https://gamma.unpythonic.net/01728224101"/>
<content type="text/html" mode="escaped">

I recently saw &lt;a href=&quot;https://beauty-of-imagination.blogspot.com/2024/10/simpler-than-pysimplegui-and-python.html&quot;&gt;a post on a blog&lt;/a&gt; about using wish as a subprocess of Python, as a way to access tk without the complexity of tkinter.

&lt;p&gt;To be clear the original post also calls out the situation where the tkinter part of python is not installed by default, and my technique would not be applicable there.

&lt;p&gt;So what do you do if you like Tk but don't care for the high level abstraction provided by Tkinter? Well, you can &lt;tt&gt;import _tkinter&lt;/tt&gt; and create a tkapp object with &lt;tt&gt;_tkinter.create()&lt;/tt&gt;.

&lt;p&gt;The _tkinter module and the tkapp object is largely undocumented, but in Python
3.11 here are some useful methods:
&lt;ul&gt;
&lt;li&gt; tkapp.createcommand: Create a callback into Python code. Takes a string and a callable. Creates a Tcl command with that name, that calls back into Python code: &lt;tt&gt;app.createcomand(&quot;cb&quot;, lambda *args: print(&quot;cb&quot;, args))&lt;/tt&gt;
&lt;li&gt; tkapp.eval: Takes a command and evaluates it. Call it with a single string: &lt;tt&gt;app.eval(&quot;button .b -text HI -command {cb arg1 arg2}&quot;)&lt;/tt&gt;
&lt;li&gt; tkapp.call: Takes a series of arguments, does proper Tcl quoting, and evaluates it: &lt;tt&gt;app.call(&quot;pack&quot;, &quot;.b&quot;)&lt;/tt&gt;
&lt;/ul&gt;
Now, go forth and program your graphical app without all that Python object
nonsense!

&lt;p&gt;&lt;pre&gt;
Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import _tkinter
&gt;&gt;&gt; app = _tkinter.create()
&gt;&gt;&gt; app.createcommand(&quot;cb&quot;, lambda *args: print(&quot;cb&quot;, args))
&gt;&gt;&gt; app.eval(&quot;button .b -text HI -command {cb arg1 arg2}&quot;)
'.b'
&gt;&gt;&gt; app.call(&quot;pack&quot;, &quot;.b&quot;)
''
&gt;&gt;&gt; # Now I click the button several times ...
&gt;&gt;&gt; cb ('arg1', 'arg2')
cb ('arg1', 'arg2')
cb ('arg1', 'arg2')
&lt;/pre&gt;
</content>
</entry>
<entry>
<title>Onscreen Crosshairs in tcl/tk</title>
<issued>2006-04-29T21:58:13Z</issued>
<modified>2006-04-29T21:58:13Z</modified>
<id>https://gamma.unpythonic.net/software/01146347893</id>
<link rel="alternate" type="text/html" href="https://gamma.unpythonic.net/software/01146347893"/>
<content type="text/html" mode="escaped">
I was staring too long at a screenshot of a graph on some web page, wishing I
could tell if two things lined up.  Well, inspiration struck, and this program
was born.  Two windows&amp;mdash;one that is one pixel wide, and another
that is one pixel tall&amp;mdash;are created, and they follow the mouse
around every 1/10 second or so.  They're displaced by one pixel so that clicking will hit the underlying window, not the crosshair.

&lt;p&gt;I apologize for the changing colors, since xor isn't available with this simple
method of drawing the lines, it's the only way to be sure to get contrast.  I considered making the color depend on e.g., whether SHIFT is pressed, but if Tk provides this information to a window that doesn't have focus, I overlooked it in the manpages.
</content>
</entry>
</feed>
