FAQ
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.

Search Discussions

  • Chris Angelico at Mar 19, 2013 at 9:39 pm

    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
  • Maiden129 at Mar 19, 2013 at 10:50 pm

    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.
  • Rick Johnson at Mar 20, 2013 at 12:57 am

    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
  • Maiden129 at Mar 20, 2013 at 1:25 am

    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.
  • Jason Swails at Mar 20, 2013 at 2:14 am

    On Tue, Mar 19, 2013 at 9:25 PM, maiden129 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-0001.html>
  • Ranting Rick at Mar 20, 2013 at 2:16 am

    On Mar 19, 8:25 pm, maiden129 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.
  • Maiden129 at Mar 20, 2013 at 2:36 am

    On Tuesday, March 19, 2013 10:16:25 PM UTC-4, Rick Johnson wrote:
    On Mar 19, 8:25 pm, maiden129 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()
  • Rick Johnson at Mar 20, 2013 at 3:06 am

    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.
  • Terry Reedy at Mar 20, 2013 at 3:21 am

    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
  • Rick Johnson at Mar 20, 2013 at 5:37 pm

    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 19, '13 at 7:01p
activeMar 20, '13 at 5:37p
posts11
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase