tkinter / gui

Discussion in 'Python' started by Rex Macey, Feb 23, 2013.

  1. Rex Macey

    Rex Macey Guest

    Here is one general and one specific question about creating GUIs using tkinter from a newbie. I have created a class in which to hold some data. I want to create a GUI to get the data from the user and store it in the object. Browsing the web I see that a lot of examples on GUIs have the forms put into classes. I'm not clear why. Why is that? Second, I've created a form with a bunch of widgets on it, one of which is a listbox. This is donewith a script, not a class. I've defined a method that responds to a Button Release event. I know this works because I can print the value selected in the listbox while within the method. However, I want the value of thelistbox after I've closed the form. How do I get that? Thanks.
     
    Rex Macey, Feb 23, 2013
    #1
    1. Advertising

  2. Rex Macey

    Rick Johnson Guest

    On Saturday, February 23, 2013 4:50:43 PM UTC-6, Rex Macey wrote:
    > Here is one general and one specific question about
    > creating GUIs using tkinter from a newbie. I have created
    > a class in which to hold some data. I want to create a
    > GUI to get the data from the user and store it in the
    > object.


    Okay...

    > Browsing the web I see that a lot of examples on GUIs have
    > the forms put into classes. I'm not clear why. Why is
    > that?


    How the heck should we know why *this* or *that* developer choose to wrap his GUI code into an object? There are both GOOD and BAD reasons for doing so. But since you failed to provide ANY example code, and more importantly, failed to provide ANY critique of such code --except of course for the /wildly/ bombastic generality of "why"-- i will be unable to provide an answer at this time.

    However, i "sense" that you have an /aversion/ to GUI components wrapped into reusable objects. If this is true, please feel free to explain "why" youfeel this way. Also feel free to provide some examples that support your line of argument. And just parroting off: "i hate any code that looks like java code", is not good enough!

    > Second, I've created a form with a bunch of widgets on it,
    > one of which is a listbox. This is done with a script,
    > not a class.


    Congratulations, you've successfully created a GUI that cannot be reused! Oh and, sorry but, you don't win any prizes, oh boo-hiss!

    > I've defined a method that responds to a Button Release
    > event. I know this works because I can print the value
    > selected in the listbox while within the method. However,
    > I want the value of the listbox after I've closed the
    > form. How do I get that?


    Simple: Save the value of the selected line EACH AND EVERY TIME the user selects a line in the listbox.

    How do you do this exactly?

    Well, you could do this by saving the value to a "module level variable" (since you mentioned using a "script"), but i think you are attacking the problem incorrectly. It would help tremendously if you would provide a very concise example of your problem. Short of that, i shall do what i do best:

    GUESS!

    This sounds an awfully lot like a dialog interface to me. Are you simply wanting to present the user with a "gui window containing forms", then have
    him "fill out the forms", and then "close the window", and then "retrieve the values"?

    If so, what you want is a dialog my friend! And this must be your lucky daypal because Tkinter provides an extendable dialog for you via the "tkinter..simpledialog.Dialog" class. Don't re-write dialog code every time!

    [Warning: the following code uses classes! *GASP*]

    ## START CODE ##
    import Tkinter as tk
    from tkSimpleDialog import Dialog as _Dialog
    from Tkconstants import END, SINGLE

    class MyListbox(tk.Listbox):
    def __init__(self, master, **kw):
    tk.Listbox.__init__(self, master, **kw)
    # Create an instance variable to save the state of
    # selected lines. A value of -1 means no selection.
    # The onerous will be on you to properly save state,
    # say when, the user clears the selection prior to
    # closing the dialog >:D. "Fools can be so cleaver!"
    self.selectedLine = -1
    self.bind("<Button-1>", self.evtButtonOneClick)

    def evtButtonOneClick(self, event):
    i = self.nearest(event.y)
    print 'User Clicked Line:', i
    self.selectedLine = i


    class MyDialog(_Dialog):
    def body(self, master):
    # Create your widgets here.
    self.listbox = lb = MyListbox(self,
    selectmode=SINGLE,
    width=20, height=10,)
    for x in range(100):
    lb.insert(END, x)
    self.listbox.pack(padx=5, pady=5)

    def validate(self):
    # Here you can validate the GUI forms and return True to
    # allow the _Dialog to process the data in the "apply"
    # method, or return False to keep the dialog open so you
    # can alert the user of his stupidity and allow him a
    # second chance at redemption.
    #
    # For now i will just return True.
    return True

    def apply(self):
    # Save any form values you want returned to you
    # into the instance variable named "self.result"
    self.result = self.listbox.selectedLine

    if __name__ == '__main__':
    def showDialog():
    dlg = MyDialog(root, title='Form Dialog')
    # The dialog will show automatically (i don't like this
    # API, but i did not write it so don't complain to me!)
    print 'Result: ', repr(dlg.result)
    root = tk.Tk()
    root.title('Main App Window')
    # Create a button so we can display the dialog multiple times
    # for testing without re-running the code each time.
    w = tk.Button(root, text='Show Dialog', command=showDialog)
    w.pack(padx=5, pady=5)
    root.mainloop()
    ## END CODE ##

    This is an example of objects being responsible for there own state in life.. if ONLY we could convince our human counterparts of the same... but i digress!

    If this example is not what you need, then please provide some disambiguation.
     
    Rick Johnson, Feb 25, 2013
    #2
    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. paul.foreman

    GUI - GUI value passing

    paul.foreman, Oct 22, 2004, in forum: Java
    Replies:
    5
    Views:
    753
    Michael Rauscher
    Oct 25, 2004
  2. ulysses
    Replies:
    4
    Views:
    768
    Werner Schiendl
    Oct 22, 2003
  3. Andrew Lapidas

    PyGTK GUI update without signals from GUI

    Andrew Lapidas, Apr 13, 2008, in forum: Python
    Replies:
    0
    Views:
    417
    Andrew Lapidas
    Apr 13, 2008
  4. Stefan Ram
    Replies:
    3
    Views:
    496
    Arne Vajhøj
    Nov 20, 2011
  5. Hal Fulton

    GUI and non-GUI data

    Hal Fulton, Aug 5, 2004, in forum: Ruby
    Replies:
    1
    Views:
    144
    Austin Ziegler
    Aug 5, 2004
Loading...

Share This Page