Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41538 > unrolled thread

Python GUI questions

Started bymaiden129 <sengokubasarafever@gmail.com>
First post2013-03-19 12:01 -0700
Last post2013-03-31 16:33 +0300
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Python GUI  questions maiden129 <sengokubasarafever@gmail.com> - 2013-03-19 12:01 -0700
    Re: Python GUI questions Chris Angelico <rosuav@gmail.com> - 2013-03-20 08:39 +1100
      Re: Python GUI questions maiden129 <sengokubasarafever@gmail.com> - 2013-03-19 15:50 -0700
      Re: Python GUI questions maiden129 <sengokubasarafever@gmail.com> - 2013-03-19 15:50 -0700
    Re: Python GUI  questions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-19 17:57 -0700
      Re: Python GUI  questions maiden129 <sengokubasarafever@gmail.com> - 2013-03-19 18:25 -0700
        Re: Python GUI questions Ranting Rick <rantingrickjohnson@gmail.com> - 2013-03-19 19:16 -0700
          Re: Python GUI questions maiden129 <sengokubasarafever@gmail.com> - 2013-03-19 19:36 -0700
            Re: Python GUI questions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-19 20:06 -0700
          Re: Python GUI questions Terry Reedy <tjreedy@udel.edu> - 2013-03-19 23:21 -0400
            Re: Python GUI questions Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-20 10:37 -0700
        Re: Python GUI questions Jason Swails <jason.swails@gmail.com> - 2013-03-19 22:14 -0400
    Re: Python GUI  questions Jan Riechers <janpeterr@freenet.de> - 2013-03-31 16:33 +0300

#41538 — Python GUI questions

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-03-19 12:01 -0700
SubjectPython GUI questions
Message-ID<4250fe44-c09c-48a8-aca2-51c1a8bc3a48@googlegroups.com>
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?

How to make an empty Entry object that will hold a single character that the user will enter?


How to A Button object with a text equal to "Count"?

Thanks in advance.

[toc] | [next] | [standalone]


#41540 — Re: Python GUI questions

FromChris Angelico <rosuav@gmail.com>
Date2013-03-20 08:39 +1100
SubjectRe: Python GUI questions
Message-ID<mailman.3536.1363729194.2939.python-list@python.org>
In reply to#41538
On Wed, Mar 20, 2013 at 6:01 AM, maiden129 <sengokubasarafever@gmail.com> 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.

This sounds like homework. Have you had a try at it yourself before
asking? If so, show us your code, and point out where the problem is;
if not, give it your best effort before you try to get someone else to
do it for you, as that's the only way to learn!

ChrisA

[toc] | [prev] | [next] | [standalone]


#41542 — Re: Python GUI questions

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-03-19 15:50 -0700
SubjectRe: Python GUI questions
Message-ID<4e436103-67b6-41af-89e7-2064b437e490@googlegroups.com>
In reply to#41540
On Tuesday, March 19, 2013 5:39:51 PM UTC-4, Chris Angelico wrote:
> On Wed, Mar 20, 2013 at 6:01 AM, 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.
> 
> 
> 
> This sounds like homework. Have you had a try at it yourself before
> 
> asking? If so, show us your code, and point out where the problem is;
> 
> if not, give it your best effort before you try to get someone else to
> 
> do it for you, as that's the only way to learn!
> 
> 
> 
> ChrisA

This is not homework, I'm trying to learn how do I create a empty object. That was my main question.

[toc] | [prev] | [next] | [standalone]


#41543 — Re: Python GUI questions

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-03-19 15:50 -0700
SubjectRe: Python GUI questions
Message-ID<mailman.3538.1363733442.2939.python-list@python.org>
In reply to#41540
On Tuesday, March 19, 2013 5:39:51 PM UTC-4, Chris Angelico wrote:
> On Wed, Mar 20, 2013 at 6:01 AM, 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.
> 
> 
> 
> This sounds like homework. Have you had a try at it yourself before
> 
> asking? If so, show us your code, and point out where the problem is;
> 
> if not, give it your best effort before you try to get someone else to
> 
> do it for you, as that's the only way to learn!
> 
> 
> 
> ChrisA

This is not homework, I'm trying to learn how do I create a empty object. That was my main question.

[toc] | [prev] | [next] | [standalone]


#41550

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-03-19 17:57 -0700
Message-ID<e745739b-a9bb-49a3-8a06-1b4ae3f4d901@googlegroups.com>
In reply to#41538
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

[toc] | [prev] | [next] | [standalone]


#41551

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-03-19 18:25 -0700
Message-ID<262b7363-f980-4926-bb36-1f795769b359@googlegroups.com>
In reply to#41550
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)

        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.



[toc] | [prev] | [next] | [standalone]


#41558 — Re: Python GUI questions

FromRanting Rick <rantingrickjohnson@gmail.com>
Date2013-03-19 19:16 -0700
SubjectRe: Python GUI questions
Message-ID<495e8e74-2211-4a82-a632-61e1cd16f854@l16g2000yqe.googlegroups.com>
In reply to#41551
On Mar 19, 8:25 pm, maiden129 <sengokubasarafe...@gmail.com> wrote:

> Here is my try to answer some of questions:
>
> [snip code]
I don't understand why you are wrapping this code into a class. Are
you trying to create something reuseable?

> I'm just struggling with only how to create an object that
> will hold a single character that the user will enter.

I would suggest that you scrape this code and start over, and a good
starting point would be at the BEGINNING.

All (well, *most*) GUI applications begin with a "root window". Once
you have created the main window you can start placing widgets inside
the window. This is the basic outline of a Tkinter GUI application.

1. Create the root window.
2. Create all the needed sub-widgets and arrange them properly.
3. Start processing user events.

So using the code you provided (and rearranging it to make sense) you
would end up with this:

## START CODE ##(Python 3.x)
import tkinter as tk
from tkinter.constants import LEFT

def cbButton(self):
    print('I should do something here!')

root = tk.Tk()
root.title("Window")
w=tk.Label(root, text="Enter a string")
w.pack(side=LEFT)
e1 = tk.Entry(root, bd=5)
e1.pack(side=LEFT)
b=tk.Button(root, text="Count", command=cbButton)
b.pack(padx=5, pady=5)
root.mainloop()
## END CODE ##

Notice that i used a more intelligent form of import that will
maintain a clean namespace. Tkinter is a very BLOATED module, and
since you are just learning you won't be aware of the names that could
cause problems.

Also notice that my style is consistent. Not following an accepted
coding style is condemnable, however, being inconsistent with your
style is abominable!

PS: Also, trim those excessive quotes please.

[toc] | [prev] | [next] | [standalone]


#41560 — Re: Python GUI questions

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-03-19 19:36 -0700
SubjectRe: Python GUI questions
Message-ID<e5f572a7-9e30-401e-b3a6-c4bf4dceeb83@googlegroups.com>
In reply to#41558
On Tuesday, March 19, 2013 10:16:25 PM UTC-4, Rick Johnson wrote:
> On Mar 19, 8:25 pm, maiden129 <sengokubasarafe...@gmail.com> wrote:



So should I redo my other code that I created with the radioButtons to change the colors of a text?

from tkinter import *

class buttons:
    def __init__(self):
        window = Tk()
        window.title("Radio buttons and buttons")
        
        self.var = IntVar()
        

        w1 =  Radiobutton(window, text="Red", variable=self.var, value=1,command=changeColor).pack()
        w2 =  Radiobutton(window, text="Yellow", variable=self.var, value=2,command=changeColor).pack()
        w3 =  Radiobutton(window, text="White", variable=self.var, value=3,command=changeColor).pack()
        w4 =  Radiobutton(window, text="Grey", variable=self.var, value=4,command=changeColor).pack()
        w5 =  Radiobutton(window, text="Green", variable=self.var, value=5,command=changeColor).pack()
        B1 = Button ( window, text="<=", command=LEFT).pack()
        B2 = Button ( window, text="=>", command=RIGHT).pack()

    def changeColor(self):
        self.var.get()
        

        
        window.mainloop()


buttons()


[toc] | [prev] | [next] | [standalone]


#41561 — Re: Python GUI questions

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-03-19 20:06 -0700
SubjectRe: Python GUI questions
Message-ID<96c80660-a4ec-41ad-9205-4d615d452229@googlegroups.com>
In reply to#41560
On Tuesday, March 19, 2013 9:36:28 PM UTC-5, maiden129 wrote:

> So should I redo my other code that I created with
> the radioButtons to change the colors of a text?

I believe so. Although you really should explain what your trying to achieve with this code. There is nothing wrong with wrapping some widgets into a single reusable class, however, your examples look more like more "using a hammer to drive screws".

For instance, you have two classes named "button" and "word" (psst: those identifiers should start with a capitol letter BTW!) which are basically two independent Tkinter root windows (even though they don't inherit from Tkinter.Tk). 

"Tkinter.Tk" is meant to be used as the first window, from there you can add as many sub-frames, sub-widgets, or even sub-windows (instances of Tkinter.Toplevel) as you want. 

If you want more than one window, for your GUI application, then instance as many "Tkinter.Toplevel" windows as you like AFTER you create the ONE AND ONLY Tkinter.Tk window.

If however you want to "group" a number of widgets inside a single window, then use the "Tkinter.Frame" to hold them.

[toc] | [prev] | [next] | [standalone]


#41562 — Re: Python GUI questions

FromTerry Reedy <tjreedy@udel.edu>
Date2013-03-19 23:21 -0400
SubjectRe: Python GUI questions
Message-ID<mailman.3542.1363749686.2939.python-list@python.org>
In reply to#41558
On 3/19/2013 10:16 PM, Ranting Rick wrote:
> import tkinter as tk
> from tkinter.constants import LEFT
>
> def cbButton(self):
>      print('I should do something here!')
>
> root = tk.Tk()
> root.title("Window")
> w=tk.Label(root, text="Enter a string")
> w.pack(side=LEFT)
> e1 = tk.Entry(root, bd=5)
> e1.pack(side=LEFT)
> b=tk.Button(root, text="Count", command=cbButton)
> b.pack(padx=5, pady=5)
> root.mainloop()

when I run this, and click the button, I get
Exception in Tkinter callback
Traceback (most recent call last):
   File "C:\Programs\Python33\lib\tkinter\__init__.py", line 1442, in 
__call__
     return self.func(*args)
TypeError: cbButton() missing 1 required positional argument: 'self'

when I remove 'self' from cbButton, I get the expected
I should do something here!

When I enter something in the Entry box and hit return, nothing happens. 
I presume that is because the example is not complete.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#41609 — Re: Python GUI questions

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-03-20 10:37 -0700
SubjectRe: Python GUI questions
Message-ID<73649809-30f8-4d9e-9247-7d1d0d26af5c@googlegroups.com>
In reply to#41562
On Tuesday, March 19, 2013 10:21:06 PM UTC-5, Terry Reedy wrote:
> On 3/19/2013 10:16 PM, Ranting Rick wrote:
> > [snip code]
> 
> when I run this, and click the button, I get:
>
>    TypeError: cbButton() missing 1 required positional argument: 'self'
> 
> ...when I remove 'self' from cbButton, I get the expected
> I should do something here!

Oops :-), good catch Terry! I had copy-pasted the OP's original method however i forgot to remove the "self" parameter; then i had to convert my Python2.x code into 3.x code, which was not terribly difficult, however, this "pit stop" was distracting enough that I forgot to click the button while testing. 

> When I enter something in the Entry box and hit return,
> nothing happens.  

My example does not bind the "KeyPress-ReturnKey" to any callback, so what were you expecting to happen when you pressed enter?

> I presume that is because the example is not complete.

Well it's as complete as it needs to be :-). Any more complete and it would less of a teaching exercise and more of a "copy-paste" exercise. I did not want to just gift wrap an answer for the OP. My hope is that he has the basic skills to turn that incomplete example into useable code. If not, well, then he needs to study that tutorial link i sent early in the thread. 

[toc] | [prev] | [next] | [standalone]


#41559 — Re: Python GUI questions

FromJason Swails <jason.swails@gmail.com>
Date2013-03-19 22:14 -0400
SubjectRe: Python GUI questions
Message-ID<mailman.3541.1363745651.2939.python-list@python.org>
In reply to#41551

[Multipart message — attachments visible in raw view] — view raw

On Tue, Mar 19, 2013 at 9:25 PM, maiden129 <sengokubasarafever@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

[toc] | [prev] | [next] | [standalone]


#42370

FromJan Riechers <janpeterr@freenet.de>
Date2013-03-31 16:33 +0300
Message-ID<mailman.4014.1364737881.2939.python-list@python.org>
In reply to#41538
On 19.03.2013 21:01, 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?
>
> How to make an empty Entry object that will hold a single character that the user will enter?
>
>  [..]
>

Hello,

here is a very good documentation about Tkinter and its most likely that 
the same principals of coding apply to Python 3.x when it comes down to 
the Tkinter part:
http://www.pythonware.com/library/tkinter/introduction/index.htm

Also as an tip, you can make use of a StringVar object for example - 
those are (if Im not completely wrong) meant to hold values which you 
can then access from the interface. Alike a binding to any element 
holding text. Which should take care of the updating part if you call a 
proper "StringVar/yourVariableName".set method of that particular class.

As for the UI creation, have a look at that documentation, its very easy 
to navigate inside if you know what you are looking for.

Regards
Jan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web