Why is my Tkintering so bad?

Robert Roy rjroy at takingcontrol.com
Sat Jun 3 11:00:50 EDT 2000


On Sat, 03 Jun 2000 04:24:23 GMT, "dsavitsk" <dsavitsk at e-coli.net>
wrote:

>example 1 does not work, example 2 does. anybody know why?
>more importantly, i need to read text from a ScrolledText (Pmw) and i can't
>do it.  The code i am attempting to use (or at least the relevant part) is
>in Example 3.  i am used to things like...
>x = Form.Text1.text (vb) so this is really foreign. essentially there is a
>text box and a button. at startup the text box reads from a text file (this
>works fine), then when the user pushes the button the contents of the text
>box are written to the output window.  It is this step of connecting code to
>events to visuals that i am not getting. any suggestions at all will be a
>big help and greatly appreciated.
>
>thanks,
>doug
>
## cut some stuff....

Your main problem here is variable scoping. st is defined int the
scope of MakeInterface and is not global. Thus it is not visible to
SaveEdits and SaveEdits will raise an exception.

If you want to do things this way then declare the variable st at the
module level and then use a GLOBAL declaration in the MakeInterface
function. Essentially the rules for global variables can be distilled
down to they can be read by any function in the module but must be
declared as GLOBAL in any function that wishes to assign to them
otherwise a new local variable is instantiated.

So by adding these two lines, your code should work.

That said, it is not a very pythonic way to do things.  Most python
programmers will create a class that encapsulates the behaviour. See
below for an example that demonstrates this.


># Example 3 ---------------------------------------
>from Tkinter import *
>import Pmw
>

st = None

>def SaveEdits():
>    x = st.get()
>    print x
>
>def MakeInterface():
	# this will tell the function to use the globally defined "st"
	# otherwise st will be local to this function
	GLOBAL st

>    flt = Frame(root)
>    st = Pmw.ScrolledText(flt, labelpos=N, label_text='Stuff',
>text_wrap='none')
>    st.importfile('TestData.txt')
>    st.pack(fill=BOTH, expand=1, padx=5, pady=5)
>
>    flt.pack(side=TOP, expand=YES, fill=X)
>
>    flb = Frame(fl)
>    cmdSave = Button(flb, width=20, text='Save Edits', borderwidth=3,
>relief=GROOVE, command=SaveEdits).pack(side=TOP, pady=5)
>    flb.pack(side=TOP, expand=YES, fill=X)
>
>root = Tk()
>root.title('window title')
>MakeInterface()
>root.mainloop()
>
>



from Tkinter import *
import Pmw
import shutil

class EditWidget:
    def __init__(self, parent, editfile):
        self.editfile = editfile
        self.bakfile = editfile + '.bak'
        flt = Frame(parent)
        self.scrolledtext = st = Pmw.ScrolledText(flt, labelpos=N,
label_text='Stuff',text_wrap='none')
        st.importfile(editfile)
        st.pack(fill=BOTH, expand=1, padx=5, pady=5)
    
        flt.pack(side=TOP, expand=YES, fill=X)
    
        flb = Frame(flt)
        cmdSave = Button(flb, width=20, text='Save Edits',
borderwidth=3,relief=GROOVE, command=self.SaveEdits)
        cmdSave.pack(side=TOP, pady=5)
        flb.pack(side=TOP, expand=YES, fill=X)

    def SaveEdits(self):
        x = self.scrolledtext.get()
        print x
        shutil.copyfile(self.editfile, self.bakfile)
        open(self.editfile,'w').write(x)
        
if __name__ == "__main__":        
    root = Tk()
    root.title('window title')
    ew = EditWidget(root,'TestData.txt')
    root.mainloop()




More information about the Python-list mailing list