[Tutor] Tkinter GUI w/o DOS window

David Jansen djansen@pobox.com
Sun, 6 Aug 2000 17:41:26 +0900


I went through Alan Gauld's tutorial a few weeks back and the last section
shows us a Tkinter GUI for a simple word counting program. It works fine but
everytime I run the program, an empty DOS window pops up in the
background... Is there a way to fire up the GUI by itself? I realize I could
run it from Pythonwin or IDLE but that wouldn't really solve anything. I
know this is an OS specific problem but maybe someone else is still using
Windows?

Thank you,
David Jansen





from Tkinter import *
import document

#########################Class Definitions###############################
class GrammarApp(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.type = 2 #create variable with default value
        self.buildUI()

    def buildUI(self):
        # First create the application window with title
        fApp = Frame(0)
        fApp.wm_title = "Grammar checker"

        #Now the file information: File name and type
        fFile = Frame(fApp)
        Label(fFile, text = "Filename: ").pack(side = "left")
        self.eName = Entry(fFile)
        self.eName.insert(INSERT, "test.htm")
        self.eName.pack(side = "left", padx = 5)

        # to keep the radio buttons lined up with the
        # name we need another frame
        fType = Frame(fFile, borderwidth = 1, relief = SUNKEN)
        self.rText = Radiobutton(fType, text = "TEXT", variable = self.type,
\
                                 value = 2, command = self.doText)
        self.rText.pack(side = TOP)
        self.rHTML = Radiobutton(fType, text = "HTML", variable = self.type,
\
                                 value = 1, command = self.doHTML)
        self.rHTML.pack(side = TOP)
        # make text the default selection
        self.rText.select()
        fType.pack(side = "right", padx = 3)
        fFile.pack(side = "top", fill = X)

        # the text box holds the output, pad it to give a border
        self.txtBox = Text(fApp, width = 60, height = 10)
        self.txtBox.pack(side = TOP, padx = 3, pady = 3)

        # finally put some command buttons on to do the real work
        fButts = Frame(fApp)
        self.bAnal = Button(fButts, text = "Analyze", command =
self.AnalyzeEvent)
        self.bAnal.pack(side = LEFT, anchor = W, padx = 50, pady = 2)
        self.bReset = Button(fButts, text = "Reset", command = self.doReset)
        self.bReset.pack(side = LEFT, padx = 10)
        self.bQuit = Button(fButts, text = "Quit", command =
self.doQuitEvent)
        self.bQuit.pack(side = RIGHT, anchor = E, padx = 50, pady = 2)

        fButts.pack(side = BOTTOM, fill = X)
        fApp.pack()

###############Event handling methods###################
    # time to die...
    def doQuitEvent(self):
        import sys
        sys.exit()

    # restore default settings
    def doReset(self):
        self.txtBox.delete(1.0, END)
        self.rText.select()

    # set radio values
    def doText(self):
        self.type = 2

    def doHTML(self):
        self.type = 1

    # Create the appropriate document type and analyze it
    # then display the results in the form
    def AnalyzeEvent(self):
        filename = self.eName.get()
        if filename == "":
            self.txtBox.insert(END, "\nNo filename provided!\n")
            return
        if self.type == 2:
            doc = document.TextDocument(filename)
        else:
            doc = document.HTMLDocument(filename)
        self.txtBox.insert(END, "\nAnalyzing...\n")
        doc.Analyze()
        str = doc.format % (filename, doc.c_paragraph,
                            doc.c_line, doc.c_sentence,
                            doc.c_clause, doc.c_words)
        self.txtBox.insert(END, str)

myApp = GrammarApp()
myApp.mainloop()