[Tutor] Tkinter dilemas

David Talaga dtalaga at novodynamics.com
Tue Apr 20 12:47:58 EDT 2004


Actually the clean method does.  I am not to sure if it is not supposed to
but that aspect of the progam is tested. And as far as the ). I found that
out no big deal. I am an idiot :) what the problem is though now is that
after I select a file to be cleaned, it cleans it and then i can not get
back to the Open() function.  I tried to call it from the bottom of the
Clean() function ut still, nothing...  Here is the new version of the
program: (It is still in the works. tinkering around with it and al)

import os
import tkFileDialog
import sys
import re
import Tkinter

#---------------------------------------------------------------------------
------------
#Define all variables
global root
root=Tkinter.Tk()
result = 0
sub = re.sub
#---------------------------------------------------------------------------
------------

# define the dialog function to call the dialog box
def dialog():
    root.withdraw()
    initDir = 'C:/windows/desktop'
    filetype = [('All files', '.*')]
    fileName = tkFileDialog.askopenfilename(initialdir=initDir,
filetypes=filetype)
    return fileName

#---------------------------------------------------------------------------
------------
#Define the doDialog function. Uses the dialog function from another file
def doDialog():
   global result
   result = dialog()

#---------------------------------------------------------------------------
------------
#Define the List function that displays the list of "cleaned" files
def Open():
    global f
    f = Tkinter.Button(root, text = "Find Files", command = Clean)
    f.pack()
    global l
    l = Tkinter.Button(root, text = "View List", command = List)
    l.pack()
    global x
    x = Tkinter.Button(root, text = "Exit", command = sys.exit)
    x.pack()

#---------------------------------------------------------------------------
------------
#Define function List to be used in the Open() function.
def List():
   S = Tkinter.Scrollbar()
   global L
   L = Tkinter.Listbox()
   S.pack(side = Tkinter.RIGHT, fill = Tkinter.Y)
   L.pack(side = Tkinter.LEFT, fill = Tkinter.Y)
   S.config(command = L.yview)
   L.config(yscrollcommand = S.set)
   os.chdir('C:\Cleaned_Files')
   Populate()

#---------------------------------------------------------------------------
---------
#Define the function to populate the listbox
def Populate():
    if os.path.isfile('cleaned_files.txt'):
        print "cleaned_files.txt exists"
    else:
        file('cleaned_files.txt', 'a', 1)
    clean = open('cleaned_files.txt', 'r+')
    clean.write(fileName)
    clean.close()

#---------------------------------------------------------------------------
---------
#Define the Clean function to clean the files
def Clean():
    if f:
        global fileName
        fileName = dialog()
        in_file = open(fileName,'r').readlines()
        out_file = open(fileName + '_cleaned.txt', 'w')
        for i in in_file:
          tmp = sub('\x0D','', i)
          out_file.write(tmp)
        print 'File cleaned'

    if x: out_file.close()

    if l: List()

    print fileName

#---------------------------------------------------------------------------
------------
#Call the working function
Open()



#---------------------------------------------------------------------------
------------
#Make the path where the cleaned files will be stored
if os.path.isdir("C:\\Cleaned_Files"):
   print "C:\\Cleaned_Files exists"
else:
   os.mkdir("C:\\Cleaned_Files")

#---------------------------------------------------------------------------
------------

root.mainloop()

  -----Original Message-----
  From: Crabtree, Chad [mailto:Chad.Crabtree at nationalcity.com]
  Sent: Tuesday, April 20, 2004 12:42 PM
  To: 'David Talaga'
  Cc: 'tutor at python.org'
  Subject: RE: [Tutor] Tkinter dilemas



    -----Original Message-----
    From: David Talaga [mailto:dtalaga at novodynamics.com]
    Sent: Tuesday, April 20, 2004 11:51 AM
    To: Python Tutor
    Subject: [Tutor] Tkinter dilemas


    I am working with Tkinter and trying to create a box that has three
buttons (in the Open() function) and these buttons seem to not work.  I am
wondering what I am doing wrong.  Here is all of the code but I think that
the problem lies within the Open function.  The first window pops up upon
running the program but there is nothing in the window. Can you help!!!

    #Created by dtalaga for Novo Dynamics
    import os
    from Tkinter import *
    import tkFileDialog
    import sys
    import re
    import Tkinter


#---------------------------------------------------------------------------
------------
    #Define all variables
    global root
    root=Tk()
    result = 0
    sub = re.sub

#---------------------------------------------------------------------------
------------

    # define the dialog function to call the dialog box
    def dialog():
        root.withdraw()
        initDir = 'C:/windows/desktop'
        filetype = [('All files', '.*')]
        fileName = tkFileDialog.askopenfilename(initialdir=initDir,
filetypes=filetype)
        return fileName


#---------------------------------------------------------------------------
------------
    #Define the doDialog function. Uses the dialog function from another
file
    def doDialog():
       global result
       result = dialog()


#---------------------------------------------------------------------------
------------
    #Define the List function that displays the list of "cleaned" files
    def Open():
       f = Tkinter.Button(root, text = "Find Files", command = Clean)
       f.pack()
       l = Tkinter.Button(root, text = "View List", command = List)
       l.pack()
       x = Tkinter.Button(root, text = "Exit", command = sys.exit)
       x.pack

    Right here you forgot to put the parenthesis on pack


#---------------------------------------------------------------------------
------------
    #Define function List to be used in the Open() function.
    def List():
       S = Tkinter.Scrollbar()
       L = Tkinter.Listbox()
       S.pack(side = Tkinter.RIGHT, fill = Tkinter.Y)
       L.pack(side = Tkinter.LEFT, fill = Tkinter.Y)
       S.config(command = L.yview)
       L.config(yscrollcommand = S.set)


#---------------------------------------------------------------------------
------------
    #Define function Clean.  Clean will take the files and clean them

    def Clean():
        if f:
           fileName = dialog()
           in_file = open(fileName,'r').readlines()
           out_file = open(fileName + '_cleaned.txt', 'w')
           for i in in_file:
             tmp = sub('\x0D','', i)
             out_file.write(tmp)
           print 'File cleaned'

        if x:
           out_file.close()
        if l:
           List()
        print fileName


#---------------------------------------------------------------------------
------------
    #Call the working functions
    #Open()

     When I uncommented Open() it ran and showed the buttons with the pack
problem above it showed all buttons


#---------------------------------------------------------------------------
------------
    #Make the path where the cleaned files will be stored
    if os.path.isdir("C:\\Cleaned_Files"):
       print "C:\\Cleaned_Files exists"
    else:
       os.mkdir("C:\\Cleaned_Files")


#---------------------------------------------------------------------------
------------

    root.mainloop()



    David Talaga
    dtalaga at novodynamics.com
    734-205-9127



    Your clean method will not work.  Look on activestate for a shim recipie
so you can pass arguments with tkinter.   Well I googled I know there is one
on AS but here is a url for another page.  Look this over you should be able
to figure this out.  I needed a similar thing in the past.

    http://www.astro.washington.edu/owen/TkinterSummary.html#CallbackShims

    Good luck

  --------------------------------------------------------------------------
-----------------
  ***National City made the following annotations
  --------------------------------------------------------------------------
-----------------

  This communication is a confidential and proprietary business
communication. It is intended solely for the use of the designated
recipient(s). If this communication is received in error, please contact the
sender and delete this communication.

============================================================================
===============
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040420/c25d5d26/attachment-0001.html


More information about the Tutor mailing list