Python GUI questions

Jason Swails jason.swails at gmail.com
Wed Mar 20 03:14:07 CET 2013


On Tue, Mar 19, 2013 at 9:25 PM, maiden129 <sengokubasarafever at gmail.com>wrote:

> On Tuesday, March 19, 2013 8:57:42 PM UTC-4, Rick Johnson wrote:
> > On Tuesday, March 19, 2013 2:01:24 PM UTC-5, maiden129 wrote:
> >
> > > Hello,
> >
> > >
> >
> > > I'm using python 3.2.3 and I'm making a program that show
> >
> > > the of occurrences of the character in the string in
> >
> > > Tkinter.
> >
> > >
> >
> > > My questions are:
> >
> > >
> >
> > > How can I make an empty Entry object that will hold a word
> >
> > > that a user will enter?
> >
> >
> >
> > I believe you meant to say: "How can i CREATE an entry field to
> accommodate user input?"
> >
> >
> >
> > Easy.
> >
> >
> >
> > > How to make an empty Entry object that will hold a single
> >
> > > character that the user will enter?
> >
> >
> >
> > Not as easy, but still quite doable. Do you want to filter the input,
> allowing only a single character?
> >
> >
> >
> > > How to A Button object with a text equal to "Count"?
> >
> >
> >
> > Easy-pee-see. Follow this yellow brick road to enlightenment.
> >
> >
> >
> >   http://effbot.org/tkinterbook/tkinter-whats-tkinter.htm
>
> Hello,
>
> Here is my try to answer some of questions:
>
>
> from tkinter import *
>
> class word:
>     def __init__(self,Entry,Character):
>         window = Tk()
>         window.title("Widget")
>
>         top = Tk()
>         L1 = Label(top, text="Enter a string")
>         L1.pack( side = LEFT)
>         E1 = Entry(top, bd =5)
>

This is unlikely to work.  You have overwritten the Entry widget from
tkinter, meaning that E1 will not be an Entry (unless you pass
tkinter.Entry to a word() instance, which seems redundant).

My suggestion is actually to generate classes derived from tkinter widgets
(I often use Frame, since it's quite generic and can act as a container for
any other widget easily).


>         E1.pack(side = RIGHT)
>
>         top.mainloop()
>
>         L2 = Label(bottom, text="Number of single characters")
>         L2.pack( side = LEFT)
>         E2 = Entry(bottom, bd =5)
>
>         button = Tkinter.Button(bottom, text ="Count", command =
>  countCharacter).pack()
>
>         def countChacater(self):
>             count = word.count(character)
>
> I'm just struggling with only how to create an object that will hold a
> single character that the user will enter.
>

This is tricky.  The approach I would take is to generate an entry widget
and then bind all key-press events in that widget to a method that checks
how long the input string is.  If it is longer than a single character,
reject the new letter and optionally raise an alert (using, e.g.,
tkMessageBox.showwarning).

If you want the count on the button to be updated continuously, you'll need
to update that counter every time either the input string or character is
changed.

Good luck,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130319/9836ea35/attachment.html>


More information about the Python-list mailing list