GUI text input window for Python

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

GUI text input window for Python

Postby Phasma Felis » Sun Aug 07, 2011 2:25 am UTC

I'm learning Python, and I'm working on a mainly command-line-oriented program that I'd like to have pop up a single text-input window--just an editable text field and a "Submit" button--and then return the value to the main program. Ideally this would all be wrapped up in a convenient function.

I'm having a bitch of a time making it work. I've started reading through this Tkinter tutorial, and I've played with various snippets like this one and this one. I either can't get the text window to close when I hit the button, or can't manage to get the text string to return. Part of it's that I'm not really grasping the way TCL works... Anyway I feel like this really ought to be easy, and I'm missing something stupid. Any suggestions, links, snippets?
Phasma Felis
 
Posts: 164
Joined: Thu May 22, 2008 3:42 am UTC

Re: GUI text input window for Python

Postby thoughtfully » Sun Aug 07, 2011 3:11 am UTC

What exactly isn't working for you? Those tutorials worked for whoever wrote them. Show us your code and what goes wrong.

You should also have a good Tkinter reference. I really like the Grayson book, but it's out print, I believe, and you don't need to invest in a book for a one-off task. There's a page of resources at the Python Wiki; "Introduction.." by Fredrik Lundh is the online reference I use the most.
http://wiki.python.org/moin/TkInter
http://www.pythonware.com/library/tkinter/introduction/

Lundh also has a bloggy sort of site with lots of Python code, a good portion of which uses Tkinter: http://effbot.org/

Therer's a lot of Tkinter tutorials and example code out there; if what you're using isn't working for you, then keep googling!

A little Tcl background is handy, but mostly for the familiar feel. You really don't need to concern yourself with that aspect.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 2092
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN

Re: GUI text input window for Python

Postby Phasma Felis » Sun Aug 07, 2011 3:28 am UTC

thoughtfully wrote:What exactly isn't working for you? Those tutorials worked for whoever wrote them. Show us your code and what goes wrong.

The tutorials do work as advertised; it's changing them from "display input onscreen" to "return input as a string" that I'm having trouble with. Sorry, I know it's bad form to ask for code help without posting the problem code, but none of the various ways I've tried modding those tutorials have gotten any closer to doing what I want, so I'm not sure seeing my fumbling additions will tell you much. I will check out those links and keep experimenting.
Phasma Felis
 
Posts: 164
Joined: Thu May 22, 2008 3:42 am UTC

Re: GUI text input window for Python

Postby thoughtfully » Sun Aug 07, 2011 5:04 am UTC

I think see what your problem is. You maybe aren't used to the nonlinear (at from a high-level view) flow of GUI code. That call to mainloop() doesn't typically return until your program is done. The program really consists of waiting for something to happen in the GUI, responding, and then waiting some more. If there's more going on (such as networking or other I/O) that gets handled by another thread or set timeouts. To do something with the user input when they active a button, you need to put your code into the callback function (or replace it with your own), such as fetch() in the example with the Quitter class that you linked to. The usual approach is to put your code into functions and then call the bit to get started from the callback with the string as a parameter. Something like this:
Code: Select all
def fetch():
    dostuff(ent.get())


Now, there's nothing stopping you from waiting until mainloop() returns and then working on the value after the GUI is done. This might make sense for a simple user interface that doesn't do much except get some input before the real processing begins. You might have tried calling ent.get() after the mainloop exits. Whether or not calling a widget method outside the mainloop works isn't always predictable. It might just depend on the underlying Tcl. You can fix that by setting a global variable or otherwise storing the value somewhere the rest of the program can access it while you are in the callback.

One more thing. You will see this line a lot:
Code: Select all
from Tkinter import *

It'd be better if you didn't copy that verbatim. Wildcard imports are an invitation for trouble. They copy every binding (variable or function to use looser terms) from the Tkinter module and pile it all into your program's namespace. You will get name collisions if you use one of those symbols yourself or if you have a second wildcard import that has some of the same names in it. Either just import the symbols you need:
Code: Select all
from Tkinter import Tk, Frame, Button

or import the module and reference its symbols through it:
Code: Select all
 import Tkinter
root = Tkinter.Tk()

or, as I prefer to do:
Code: Select all
import Tkinter as tk
root = tk.Tk()
Last edited by thoughtfully on Sun Aug 07, 2011 5:13 am UTC, edited 2 times in total.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 2092
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN

Re: GUI text input window for Python

Postby Anonymously Famous » Sun Aug 07, 2011 5:11 am UTC

I'm kind of new when it comes to Python, too, but I'll take a look when I get the chance.
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: GUI text input window for Python

Postby laranzu » Sun Aug 07, 2011 9:34 am UTC

Phasma Felis wrote:... Part of it's that I'm not really grasping the way TCL works... Anyway I feel like this really ought to be easy, and I'm missing something stupid. Any suggestions, links, snippets?

I suggest not using Tkinter. As you've noticed, it's designed for how TCL works, not Python.

My recommendation would be wxPython. If you're on Linux, GTK has a fairly good Python library these days.
User avatar
laranzu
 
Posts: 103
Joined: Fri Nov 30, 2007 4:21 pm UTC
Location: Physical: Canberra, Australia

Re: GUI text input window for Python

Postby thoughtfully » Sun Aug 07, 2011 11:05 am UTC

I think Tkinter is a lot easier to learn/use, and it's included. One of those others is better for serious use later, but that can wait.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 2092
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN

Re: GUI text input window for Python

Postby Anonymously Famous » Sun Aug 07, 2011 3:44 pm UTC

So, as I understand it, You're doing stuff on the console, and you want to be able to fetch a value from some sort of dialog. And you want the code for displaying the dialog wrapped inside a function call.

Will something like this do?
Code: Select all
import Tkinter as tk

class MyDialog:
    def __init__(self, parent):

        top = self.top = tk.Toplevel(parent)

        tk.Label(top, text="Please enter a value:").pack()

        self.e = tk.Entry(top)
        self.e.pack(padx=5)

        b = tk.Button(top, text="OK", command=self.ok)
        b.pack(pady=5)
        self.value = ""

    def ok(self):

        self.value = self.e.get()

        self.top.destroy()

def returnValueFunction():
    root = tk.Tk()
    d = MyDialog(root)
    root.wait_window(d.top)
    return d.value

# Do the console stuff for your program here

myValue = returnValueFunction()

print "The value that you entered into the dialog was: ", myValue


This was modified from this example on how to create a simple dialog.

Oh yeah, I used this guy's advice, too, to avoid importing an asterisk:
thoughtfully wrote:or, as I prefer to do:
Code: Select all
import Tkinter as tk
root = tk.Tk()
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: GUI text input window for Python

Postby thoughtfully » Sun Aug 07, 2011 5:03 pm UTC

You may find using the Stringvar class to be more convenient.
http://effbot.org/tkinterbook/entry.htm
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 2092
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN


Return to Coding

Who is online

Users browsing this forum: No registered users and 6 guests