#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Tkinter inpput box


    i have a thinker input box and a button next to it how do i get the value in the input box to become a vrablie to use in the programe?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    Originally Posted by philip_95
    i have a thinker input box and a button next to it how do i get the value in the input box to become a vrablie to use in the programe?
    If by "input box" you mean "entry widget", there is a widget option (attribute), "textvariable". If you set this to a variable name,
    Code:
    self.<entry widget path>=Entry(frame, width=somewidth, textvariable=varname)
    ,
    then presumably the value in the entry widget will be (as a string) the value of varname.

    I find it easier however to just grab the contents when I want them and assign the variable accordingly:
    Code:
    varname=self.<entry widget path>.get()
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    Criminal that we must be careful with posts while you write careless slop.


    And as rrashkin says, you didn't bother to indicate what is an "input box".
    Code:
    #Reference
    
    import sys
    
    try:
        import Tkinter as tkinter
    except:
        import tkinter
    
    top = tkinter.Tk()
    
    t = tkinter.Text(top)
    t.pack()
    t.insert('1.0','The Text widget')
    
    e = tkinter.Entry(top)
    e.pack()
    e.insert(0,'Entry widget')
    
    class callback:
    
        '''
            Hmmm.  lamba is simple.
        '''
    
        keys = set('text entry'.split())
    
        def __init__(self,**kwargs):
            # force pretty construction.
            if not self.__class__.keys.issubset(set(kwargs)):
                raise ValueError(
                    '%s(entry=EntryWidget,text=TextWidget)'%self.__class__.__name__)
            self.__dict__.update(kwargs)
            self.ouf = sys.stdout # open file ouf specifies the text and entry destination.
    
        def __call__(self,*args,**kwargs):
            w = self.ouf.write
            w('-'*40)
            w('\n'*2)
            w('Text box content:\n')
            # '1.0' specifies row 1, column 0, which is the start.
            w(self.text.get('1.0',tkinter.END))  ##########You're looking for this line
            w('\n')
            w('Entry box content: '+self.entry.get()) ################## and for this line also
            w('\n'*2)
    
    tkinter.Button(top,
                   text='write content to ouf',
                   command=callback(text=t,entry=e)
                   ).pack(side=tkinter.LEFT)
    tkinter.Button(top,text='exit',command=top.destroy).pack(side=tkinter.RIGHT)
    
    top.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Let's assume you want to do this:
    Code:
    '''tk_Entry_Button_simple.py
    simple Tkinter Entry/Button example
    '''
    
    # with Python2 use 'import Tkinter as tk'
    import tkinter as tk
    
    def action():
        # get the entry value
        val = enter.get()
        # display it in the root title as a test
        root.title("name = %s" % val)
    
    
    root = tk.Tk()
    root.title("Enter your name")
    # the entry
    enter = tk.Entry(root, bg='yellow', width=50)
    # the action button
    button = tk.Button(root, text='Show name', command=action)
    
    # pack widgets horizontally in the root window in order
    enter.pack(side='left')
    button.pack()
    
    # start cursor in entry
    enter.focus()
    
    # start the event loop
    root.mainloop()

    Comments on this post

    • philip_95 agrees : this helped
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    That is pretty cool! What if I wanted to calculate the monthly payment on an auto loan?
    Code:
    'Monthly Payment'
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    yes i mean entry box
    thanks i try some of them
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Dietrich
    Let's assume you want to do this:
    Code:
    '''tk_Entry_Button_simple.py
    simple Tkinter Entry/Button example
    '''
    
    # with Python2 use 'import Tkinter as tk'
    import tkinter as tk
    
    def action():
        # get the entry value
        val = enter.get()
        # display it in the root title as a test
        root.title("name = %s" % val)
    
    
    root = tk.Tk()
    root.title("Enter your name")
    # the entry
    enter = tk.Entry(root, bg='yellow', width=50)
    # the action button
    button = tk.Button(root, text='Show name', command=action)
    
    # pack widgets horizontally in the root window in order
    enter.pack(side='left')
    button.pack()
    
    # start cursor in entry
    enter.focus()
    
    # start the event loop
    root.mainloop()
    Thanks this helped

IMN logo majestic logo threadwatch logo seochat tools logo