Mailing List Archive

Learning Tkinter
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc
was published in 1999 and I wonder if there is a more recent version.
I've googled a bit and this version is the one I keep finding. I like
how this document is organized and also how it provides the code with
visuals of what should appear on the screen. If there are other docs I
should read, please let me know.

Second, I am trying to work through a couple of the examples and make
some small tweaks as I go to see how new things can work. In the first
case, I have copied the code in the book to see how the menu works and
are created as in the example menu.py below. I see how menus are created
and how the command option is used to call the function callback.

# menu.py
from Tkinter import *

def callback():
print "called the callback!"

root = Tk()

# create a menu
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=harold)
filemenu.add_command(label="Open...", command=callback)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback)

mainloop()

However, I now want to incorporate a basic python program with a
command. Say I have a simple program called test.py

# test.py
filename = raw_input("Please enter the file you want to open: ")
new_file = raw_input("Save the output file as: ")

f = open(new_file, 'w')
new = open(filename, 'r')

for line in new:
x = line.split('\t')
print >> f, x[0],':', x[1]
f.close()

To make this example complete assume I have a text file like this

# data.txt
1 one
2 two
3 three
4 four

So, the user currently just follows directions on the screen, enters the
file names, and I get what I want. I'd like to try experimenting with
gui programming to see if the python programs I have written can be made
even more user friendly. I currently use py2exe to create executables so
that others in my organization can use these programs.

In that spirit, say I want to have a menu option that allows the user to
search their computer for this file, execute the python code and then
save the result as a user-defined filename. So, I guess my questions are
how do I associate the portion of code in menu.py
"filemenu.add_command(label="Open...", command=callback)" with an
operation that gives the user the ability to search the drives on their
machine and then once they do let python execute the code in test.py?

Many thanks,
--
http://mail.python.org/mailman/listinfo/python-list
Re: Learning Tkinter [ In reply to ]
Re: Learning Tkinter [ In reply to ]
On Apr 16, 7:46 am, "Doran, Harold" <HDo...@air.org> wrote:
> I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc
> was published in 1999 and I wonder if there is a more recent version.
> I've googled a bit and this version is the one I keep finding. I like
> how this document is organized and also how it provides the code with
> visuals of what should appear on the screen. If there are other docs I
> should read, please let me know.


There's some good Tkinter coverage in Lutz's tome, "Programming Python
3rd Ed." and it also shows how to do a search for a file across your
file system, iirc.


>
> Second, I am trying to work through a couple of the examples and make
> some small tweaks as I go to see how new things can work. In the first
> case, I have copied the code in the book to see how the menu works and
> are created as in the example menu.py below. I see how menus are created
> and how the command option is used to call the function callback.
>
> # menu.py
> from Tkinter import *
>
> def callback():
> print "called the callback!"
>
> root = Tk()
>
> # create a menu
> menu = Menu(root)
> root.config(menu=menu)
>
> filemenu = Menu(menu)
> menu.add_cascade(label="File", menu=filemenu)
> filemenu.add_command(label="New", command=harold)
> filemenu.add_command(label="Open...", command=callback)
> filemenu.add_separator()
> filemenu.add_command(label="Exit", command=callback)
>
> helpmenu = Menu(menu)
> menu.add_cascade(label="Help", menu=helpmenu)
> helpmenu.add_command(label="About...", command=callback)
>
> mainloop()
>
> However, I now want to incorporate a basic python program with a
> command. Say I have a simple program called test.py
>
> # test.py
> filename = raw_input("Please enter the file you want to open: ")
> new_file = raw_input("Save the output file as: ")
>
> f = open(new_file, 'w')
> new = open(filename, 'r')
>
> for line in new:
> x = line.split('\t')
> print >> f, x[0],':', x[1]
> f.close()
>
> To make this example complete assume I have a text file like this
>
> # data.txt
> 1 one
> 2 two
> 3 three
> 4 four
>
> So, the user currently just follows directions on the screen, enters the
> file names, and I get what I want. I'd like to try experimenting with
> gui programming to see if the python programs I have written can be made
> even more user friendly. I currently use py2exe to create executables so
> that others in my organization can use these programs.
>
> In that spirit, say I want to have a menu option that allows the user to
> search their computer for this file, execute the python code and then
> save the result as a user-defined filename. So, I guess my questions are
> how do I associate the portion of code in menu.py
> "filemenu.add_command(label="Open...", command=callback)" with an
> operation that gives the user the ability to search the drives on their
> machine and then once they do let python execute the code in test.py?
>
> Many thanks,

It sounds like you want to run code from within your own program. This
would require embedding a Python interpreter, which is quite possible,
although I do not know how to do it. I would suggest that you just use
a Tkinter-created frame/window that allows the user to enter the
information into text controls rather than a command line type
interface. You could even use a "Browse" button and let the user
search for the file using a file dialog. Check out the sample code for
such a beast in the recipe linked below:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438123

If you do want to go the embedding route, you'll want to read the
following information linked below:

http://docs.python.org/api/embedding.html
http://www.python.org/doc/ext/embedding.html
http://www.ragestorm.net/tutorial?id=21
http://www.codeproject.com/KB/cpp/embedpython_1.aspx

Hope that gets you going.

Mike
--
http://mail.python.org/mailman/listinfo/python-list
Re: Learning Tkinter [ In reply to ]
On Wed, 16 Apr 2008 14:46:13 +0200, Doran, Harold <HDoran@air.org> wrote:
[snip]
> Second, I am trying to work through a couple of the examples and make
> some small tweaks as I go to see how new things can work. In the first
> case, I have copied the code in the book to see how the menu works and
> are created as in the example menu.py below. I see how menus are created
> and how the command option is used to call the function callback.
>
> # menu.py
> from Tkinter import *
>
> def callback():
> print "called the callback!"
>
> root = Tk()
>
> # create a menu
> menu = Menu(root)
> root.config(menu=menu)
>
> filemenu = Menu(menu)
> menu.add_cascade(label="File", menu=filemenu)
> filemenu.add_command(label="New", command=harold)
> filemenu.add_command(label="Open...", command=callback)
> filemenu.add_separator()
> filemenu.add_command(label="Exit", command=callback)
>
> helpmenu = Menu(menu)
> menu.add_cascade(label="Help", menu=helpmenu)
> helpmenu.add_command(label="About...", command=callback)
>
> mainloop()
>
> However, I now want to incorporate a basic python program with a
> command. Say I have a simple program called test.py
>
> # test.py
> filename = raw_input("Please enter the file you want to open: ")
> new_file = raw_input("Save the output file as: ")
>
> f = open(new_file, 'w')
> new = open(filename, 'r')
>
> for line in new:
> x = line.split('\t')
> print >> f, x[0],':', x[1]
> f.close()
>
> To make this example complete assume I have a text file like this
>
> # data.txt
> 1 one
> 2 two
> 3 three
> 4 four
>
> So, the user currently just follows directions on the screen, enters the
> file names, and I get what I want. I'd like to try experimenting with
> gui programming to see if the python programs I have written can be made
> even more user friendly. I currently use py2exe to create executables so
> that others in my organization can use these programs.
> In that spirit, say I want to have a menu option that allows the user to
> search their computer for this file, execute the python code and then
> save the result as a user-defined filename. So, I guess my questions are
> how do I associate the portion of code in menu.py
> "filemenu.add_command(label="Open...", command=callback)" with an
> operation that gives the user the ability to search the drives on their
> machine and then once they do let python execute the code in test.py?

The way it's written, you'll have a hard time doing it... Since test.py
already includes a "user interface" via the raw_input calls, executing
test.py will always ask the user for the file names, and there's no simple
way to pass them otherwise as the module is written.

Considering how you asked the question, I'll assume you don't know Python
very much; my apologies in advance if it's not the case...

So, what I would do is rewrite the test.py script as follows:

--- test.py -----------------------------------
def convert_file(filename, new_file):
f = open(new_file, 'w')
new = open(filename, 'r')

for line in new:
x = line.split('\t')
print >> f, x[0],':', x[1]
f.close()

if __name__ == '__main__':
filename = raw_input("Please enter the file you want to open: ")
new_file = raw_input("Save the output file as: ")
convert_file(filename, new_file)
-----------------------------------------------

This is basically the same as yours in another order, and defining a
function that actually does the 'functional' part without asking the user
anything. The last block - starting with this weird 'if __name__ ==
'__main__':' - ensures the script will work as you expect when you run it
alone on the command line (with 'python test.py'). This is the meaning of
the test on __name__: this magical variable is set to the string
'__main__' if and only if the current script is the top-most one, i.e the
one you ran python on. So basically, the script now defines a function,
then asks for the function parameters and calls it _only if run as the
main script_. Try it; it should have the same behaviour as the script
you've written.

But the test on __name__ also allows to *import* this script as a module
in another script without nasty side effects. This is done via the
statement:

import test

in the other script. If test.py is written as above, this will execute the
function definition, but not the body of the 'if', since test.py is no
more run from the command line, but used in an import. If you didn't have
this test, the module would have been *executed* by the import, and is
would have asked the file names immediatly.

Once you've done the import, you can then use the function it defines by
calling test.convert_file. So now, in your GUI part, you can now write
something like:


--- menu.py ----------------------------------
## NEW! the next line gets the 'convert_file' function
import test
from Tkinter import *
## NEW! the next line gets the functions used to get file names via a
Tkinter GUI
from tkFileDialog import askopenfilename, asksaveasfilename

## NEW! the following function is the callback for the 'Open' menu entry
def open_callback():
## Get name of the input file
input_file_name = askopenfilename()
if not input_file_name:
## Dialog has been cancelled
return
## Get name for output file
output_file_name = asksaveasfilename()
if not output_file_name:
## Dialog has been cancelled
return
## Do the job
test.convert_file(input_file_name, output_file_name)

def callback():
print "called the callback!"

root = Tk()

# create a menu
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=harold)
## CHANGED! put the callback function defined above here
filemenu.add_command(label="Open...", command=open_callback)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback)

mainloop()
----------------------------------------------

Try it: it should basically do what you were asking for.

HTH
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
--
http://mail.python.org/mailman/listinfo/python-list
Re: Learning tkinter [ In reply to ]
On 2023-05-12, Rob Cliffe via Python-list <python-list@python.org> wrote:
>
> Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
> bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import tkinter
> >>> tkinter.messagebox
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: module 'tkinter' has no attribute 'messagebox'
> >>>

$ python
Python 3.11.3 (main, May 8 2023, 09:00:54) [GCC 12.2.1 20230428] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> from tkinter import messagebox
>>> messagebox
<module 'tkinter.messagebox' from '/usr/lib/python3.11/tkinter/messagebox.py'>
>>>


> Why is this?

Dunno.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Learning tkinter [ In reply to ]
On Thu, 18 May 2023 at 10:16, Rob Cliffe via Python-list
<python-list@python.org> wrote:
>
> I am trying to learn tkinter.
> Several examples on the internet refer to a messagebox class
> (tkinter.messagebox).
> But:
>
> Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
> bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import tkinter
> >>> tkinter.messagebox
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: module 'tkinter' has no attribute 'messagebox'
> >>>
>
> Why is this?

Do you have a file called tkinter.py in the current directory?

--
Oscar
--
https://mail.python.org/mailman/listinfo/python-list
Re: Learning tkinter [ In reply to ]
On Thu, 18 May 2023 at 19:15, Rob Cliffe via Python-list
<python-list@python.org> wrote:
>
> I am trying to learn tkinter.
> Several examples on the internet refer to a messagebox class
> (tkinter.messagebox).
> But:
>
> Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
> bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import tkinter
> >>> tkinter.messagebox
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: module 'tkinter' has no attribute 'messagebox'
> >>>
>
> Why is this?

Is it possible that you've created a tkinter.py for your own tinkering?

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
RE: Learning tkinter [ In reply to ]
This works for me. Hope it helps.

from tkinter import messagebox

messagebox.showerror("Hi", f"Hello World")

-----Original Message-----
From: Python-list <python-list-bounces+jschwar=sbcglobal.net@python.org> On Behalf Of Rob Cliffe via Python-list
Sent: Friday, May 12, 2023 3:55 AM
To: Python <python-list@python.org>
Subject: Learning tkinter

I am trying to learn tkinter.
Several examples on the internet refer to a messagebox class (tkinter.messagebox).
But:

Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.messagebox
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'messagebox'
>>>

Why is this?
TIA
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Learning tkinter [ In reply to ]
On 5/18/2023 9:13 AM, Grant Edwards wrote:
> On 2023-05-12, Rob Cliffe via Python-list <python-list@python.org> wrote:
>>
>> Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
>> bit (Intel)] on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import tkinter
>>>>> tkinter.messagebox
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> AttributeError: module 'tkinter' has no attribute 'messagebox'
>>>>>
>
> $ python
> Python 3.11.3 (main, May 8 2023, 09:00:54) [GCC 12.2.1 20230428] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import tkinter
>>>> from tkinter import messagebox
>>>> messagebox
> <module 'tkinter.messagebox' from '/usr/lib/python3.11/tkinter/messagebox.py'>
>>>>
>
>
>> Why is this?
>
> Dunno.

tkinter is a package, messagebox is a module within the tkinter package.
the messagebox module has some functions, such as showinfo(). You *can*
import those functions using "dot" expressions:

>>> from tkinter.messagebox import showinfo
<function showinfo at 0x0000021CED0634C0>

You can also import the entire module using the "dot" syntax:

>>> import tkinter.messagebox
>>> messagebox.showinfo
<function showinfo at 0x0000021CED0634C0>

Whether you can directly ask for tkinter.messagebox depends on whether
it's been defined or imported in tkinter/__init__.py.



--
https://mail.python.org/mailman/listinfo/python-list
Re: Learning tkinter [ In reply to ]
On 5/18/23 08:50, Jim Schwartz wrote:
> This works for me. Hope it helps.
>
> from tkinter import messagebox
>
> messagebox.showerror("Hi", f"Hello World")

It's probably instructive that IDLE always brings it in this way.

Lib/idlelib/config_key.py:from tkinter import messagebox
Lib/idlelib/configdialog.py:from tkinter import messagebox
Lib/idlelib/editor.py:from tkinter import messagebox
... etc


--
https://mail.python.org/mailman/listinfo/python-list
Re: Learning tkinter [ In reply to ]
On 2023-05-12 09:55, Rob Cliffe via Python-list wrote:
> I am trying to learn tkinter.
> Several examples on the internet refer to a messagebox class
> (tkinter.messagebox).
> But:
>
> Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32
> bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import tkinter
> >>> tkinter.messagebox
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: module 'tkinter' has no attribute 'messagebox'
> >>>
>
> Why is this?

It's a submodule of tkinter:

>>> import tkinter.messagebox as mb
>>> mb.showinfo
<function showinfo at 0x000001994C7F2020>

--
https://mail.python.org/mailman/listinfo/python-list