Thread: Create a GUI

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

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Create a GUI


    Greetings,
    I am taking a Programming class starting in January and I am working through some YouTube videos. The one I just viewed was on using Tkinter. I am working on creating a gui to calculate the monthly payment of a car loan. I just downloaded Python 3.2.3.
    I want to have 3 labels and 3 entry boxes (total loan, interest rate, term).
    I am new to this so any info is appreciated. I have attached what I learned from the YouTube video.
    Code:
    import tkinter
    
    def main():
        root = tkinter.Tk()
    
        tkinter.mainloop()
    
    if __name__ == "__main__":
        main()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Online tkinter reference click me

    Read data from Entry widget:
    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!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Thanks for the response. I think I may have come to an alternate way. What do you think of what I have come up with? Also, not sure how to calculate 'compute'
    Code:
    from tkinter import Tk, Button, Entry, Label, END
    
    from tkinter.messagebox import showinfo
    
    def compute():
        global dateEnt
        #need to calculate for monthly payment
    
    
    def clear():
        global dateEnt
        dateEnt.delete(0, END)
    
    root = Tk()
    
    label = Label(root, text='Enter Amount')
    label.grid(row=0, column=0)
    label = Label(root, text='Enter Interest')
    label.grid(row=1, column=0)
    label = Label(root, text='Enter Term')
    label.grid(row=2, column=0)
    
    dateEnt = Entry(root)
    dateEnt.grid(row=0, column=1)
    
    dateEnt = Entry(root)
    dateEnt.grid(row=1, column=1)
    
    dateEnt = Entry(root)
    dateEnt.grid(row=2, column=1)
    
    button = Button(root, text='Enter', command=compute) 
    button.grid(row=3, column=0)
    
    button = Button(root, text='Clear', command=clear) 
    button.grid(row=3, column=1)
    
    root.mainloop()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I think you're trying to stuff too many objects into your dateEnt variable. Consequently, the clear button only clears one of the Entries. Have you read about lists yet? You might be able to search for the Entry widget that has focus and clear only that one. If buttons can have focus that would annihilate this idea. I don't have time to test now. Otherwise, you're doing well.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Thanks, good point. on the clear and dataEnt
    What would I include to calculate the monthly payent?
    Code:
    def compute():
                   global dateEnt
                   ??????
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    You'd read the entries in the Entry boxes, and use your other program for the computation, displaying whatever part you intend. The code I posted earlier in this thread shows how to read data from Entry widgets.

    Also, I the Entry widgets have validation options. You could assure that the entries are valid floating point numbers.
    Originally Posted by http://effbot.org/tkinterbook/entry.htm
    validate=
    Specifies when validation should be done. You can use “focus” to validate whenever the widget gets or loses the focus, “focusin” to validate only when it gets focus, “focusout” to validate when it loses focus, “key” on any modification, and ALL for all situations. Default is NONE (no validation). (validate/Validate)
    validatecommand=
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo