simple Tkinter Question

Discussion in 'Python' started by Maboroshi, Oct 5, 2004.

  1. Maboroshi

    Maboroshi Guest

    Hi I was curious how I can pass arguments through a Tkinter Entry widget

    say I have an Entry widget and a Button widget I enter text into the
    entry and now I want the button to process the text is there a certain
    command I have to use to assign the button to the entry or vise versa

    anyhelp appreciated
    Maboroshi, Oct 5, 2004
    #1
    1. Advertising

  2. Maboroshi

    Eric Brunel Guest

    Maboroshi wrote:
    > Hi I was curious how I can pass arguments through a Tkinter Entry widget
    >
    > say I have an Entry widget and a Button widget I enter text into the
    > entry and now I want the button to process the text is there a certain
    > command I have to use to assign the button to the entry or vise versa
    >
    > anyhelp appreciated


    Sorry, but you'll have to be a bit more specific than that. What is exactly your
    problem? Getting and setting the text in the entry? Assigning an action to the
    button? What do you mean exactly by "assign the button to the entry"?
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
    Eric Brunel, Oct 5, 2004
    #2
    1. Advertising

  3. Maboroshi

    duikboot Guest

    Maboroshi wrote:
    > Hi I was curious how I can pass arguments through a Tkinter Entry widget
    >
    > say I have an Entry widget and a Button widget I enter text into the
    > entry and now I want the button to process the text is there a certain
    > command I have to use to assign the button to the entry or vise versa
    >
    > anyhelp appreciated


    Take a look at
    http://www.pythonware.com/library/tkinter/introduction/index.htm

    example

    from Tkinter import *

    def pr_text():
    text = entry.get()
    print text

    gui=Tk()
    entry=Entry(gui)
    entry.pack()
    button=Button(gui, text="print", command=pr_text)
    button.pack()

    gui.mainloop()


    --
    http://www.baandersconsultancy.nl | http://www.nosonis.com
    duikboot, Oct 5, 2004
    #3
  4. Maboroshi

    Eric Brunel Guest

    duikboot wrote:
    > Maboroshi wrote:
    >
    >> Hi I was curious how I can pass arguments through a Tkinter Entry widget
    >>
    >> say I have an Entry widget and a Button widget I enter text into the
    >> entry and now I want the button to process the text is there a certain
    >> command I have to use to assign the button to the entry or vise versa
    >>
    >> anyhelp appreciated

    >
    >
    > Take a look at
    > http://www.pythonware.com/library/tkinter/introduction/index.htm
    >
    > example
    >
    > from Tkinter import *
    >
    > def pr_text():
    > text = entry.get()
    > print text
    >
    > gui=Tk()
    > entry=Entry(gui)
    > entry.pack()
    > button=Button(gui, text="print", command=pr_text)
    > button.pack()
    >
    > gui.mainloop()


    If *that* is the actual problem, I'd use a Tkinter StringVariable to do the job:

    from Tkinter import *
    root = Tk()
    v = StringVar()
    Entry(root, textvariable=v).pack()
    def pr_text():
    print v.get()
    Button(root, text='Print', command=pr_text).pack()
    root.mainloop()

    Tkinter variables are a better choice because you often want to know what *was*
    in your entry after it has been destroyed from the display. In such a case,
    entry.get() will raise a TclError, because the entry no more exists. If you use
    a variable, it will still be there after the entry was destroyed, so you can
    still read its value.

    HTH
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
    Eric Brunel, Oct 5, 2004
    #4
  5. Maboroshi

    duikboot Guest

    Eric Brunel wrote:
    > duikboot wrote:
    >
    >> Maboroshi wrote:
    >>
    >>> Hi I was curious how I can pass arguments through a Tkinter Entry widget
    >>>
    >>> say I have an Entry widget and a Button widget I enter text into the
    >>> entry and now I want the button to process the text is there a
    >>> certain command I have to use to assign the button to the entry or
    >>> vise versa
    >>>
    >>> anyhelp appreciated

    >>
    >>
    >>
    >> Take a look at
    >> http://www.pythonware.com/library/tkinter/introduction/index.htm
    >>
    >> example
    >>
    >> from Tkinter import *
    >>
    >> def pr_text():
    >> text = entry.get()
    >> print text
    >>
    >> gui=Tk()
    >> entry=Entry(gui)
    >> entry.pack()
    >> button=Button(gui, text="print", command=pr_text)
    >> button.pack()
    >>
    >> gui.mainloop()

    >
    >
    > If *that* is the actual problem, I'd use a Tkinter StringVariable to do
    > the job:
    >
    > from Tkinter import *
    > root = Tk()
    > v = StringVar()
    > Entry(root, textvariable=v).pack()
    > def pr_text():
    > print v.get()
    > Button(root, text='Print', command=pr_text).pack()
    > root.mainloop()
    >
    > Tkinter variables are a better choice because you often want to know
    > what *was* in your entry after it has been destroyed from the display.
    > In such a case, entry.get() will raise a TclError, because the entry no
    > more exists. If you use a variable, it will still be there after the
    > entry was destroyed, so you can still read its value.
    >
    > HTH


    You're absolutely right. It's better indeed.

    --
    http://www.baandersconsultancy.nl | http://www.nosonis.com
    duikboot, Oct 5, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    504
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    450
    Jeff Epler
    Aug 23, 2004
  3. Pierre Dagenais
    Replies:
    0
    Views:
    309
    Pierre Dagenais
    Aug 3, 2008
  4. Hidekazu IWAKI
    Replies:
    1
    Views:
    376
    Peter Otten
    Dec 14, 2009
  5. Hidekazu IWAKI
    Replies:
    0
    Views:
    522
    Hidekazu IWAKI
    Dec 15, 2009
Loading...

Share This Page