tkinter / gui
rantingrickjohnson at gmail.com
Mon Feb 25 05:03:12 CET 2013
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
> 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
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" you feel 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:
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 day pal 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
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
def evtButtonOneClick(self, event):
i = self.nearest(event.y)
print 'User Clicked Line:', i
self.selectedLine = i
def body(self, master):
# Create your widgets here.
self.listbox = lb = MyListbox(self,
for x in range(100):
# 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.
# Save any form values you want returned to you
# into the instance variable named "self.result"
self.result = self.listbox.selectedLine
if __name__ == '__main__':
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)
## 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.
More information about the Python-list