Python Forum

Full Version: GUI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Getting a class FileDialogDemo not found in this code. What am I doing wrong?

"""Write a GUI-based program that
allows the user to open, edit, and save text files."""

#imports breezy and EasyFrame
from breezypythongui import EasyFrame
#imports tkinter and file dialogue
from tkinter import filedialog
#imports system
import sys


class FileDialogDemo(EasyFrame):
    """Illustrates command buttons and user events."""

    def __init__(self):
        """Sets up the window, label and buttons."""
        EasyFrame.__init__(self, "File Dialog Demo")

        #a single label in the first row
        self.outputArea = self.addTextArea("", row = 0,
                                           column = 0,
                                           width = 80,
                                           height = 15)
        self.addButton(text = "Open", row = 1, column = 1,
                       command = self.openFile)
        self.addButton(text = "Save", row =1, colummn = 2,
                       command = self.saveFile)
        self.addButton(text = "New", row = 1, column = 3,
                       command = self.newFile)

        #scrollbar
        rook = Tk()
        scrollbar = Scrollbar(root)
        scrollbar.pack(side = RIGHT, fill = Y)
        fList=Listbox(root, yscrollcommand=scrollbar.set)
        fList.pack(side = LEFT, fill = BOTH)
        scrollbar.config(command = fList.yview)

        #Event handling method.
    def openFile(self):
            """Pops up an open file dialog, and if a file is selected
            displays its text in the text area and its pathname in the
            title bar."""

            fList = [("Python files", "*.py"),("Text files","*.txt")]
            fileName = tkinter.filedialog.askopenfilename(parent = self,
                                                      filetypes = fList)

            if fileName !="":
                file = open(fileName, 'r')
                text = file.read()
                file.close()
                self.outputArea.setText(text)
                self.setTitle(fileName)

    def saveFile(self, fileName=None):
            if self.__file == None:
                #save as new file
                self.__file=asksaveasfilename(intialfile="Untitled.txt",
                             defaultextenstions=".txt",filetypes=[("All Files","*.*"),
                             ("Text Documents","*.txt")])

            if self.__file=="":
                self.__file=None
            else:
                    #try to save the file
                file = open(self.__file,"w")
                file.write(self.__thisTextArea.get(1.0,END))
            
                #change the window title
            self.__root.title(os.path.basement(self.__file)+"-TextEditor")


    def newFile(self):
            self.__root.title("Untitled - TextEditor")
            self.__file = None
            self.__thisTextArea.delete(1.0,END)
            
                                                          
    def main():
        FileDialogDemo().mainloop()

    if __name__ == "__main__" :
        main()
        sys.exit()#exits system
Please use proper tags and post any errors.
"""Write a GUI-based program that
allows the user to open, edit, and save text files."""

#imports breezy and EasyFrame
from breezypythongui import EasyFrame
#imports tkinter and file dialogue
from tkinter import filedialog
#imports system
import sys


class FileDialogDemo(EasyFrame):
    """Illustrates command buttons and user events."""

    def __init__(self):
        """Sets up the window, label and buttons."""
        EasyFrame.__init__(self, "File Dialog Demo")

        #a single label in the first row
        self.outputArea = self.addTextArea("", row = 0,
                                           column = 0,
                                           width = 80,
                                           height = 15)
        self.addButton(text = "Open", row = 1, column = 1,
                       command = self.openFile)
        self.addButton(text = "Save", row =1, colummn = 2,
                       command = self.saveFile)
        self.addButton(text = "New", row = 1, column = 3,
                       command = self.newFile)

        #scrollbar
        rook = Tk()
        scrollbar = Scrollbar(root)
        scrollbar.pack(side = RIGHT, fill = Y)
        fList=Listbox(root, yscrollcommand=scrollbar.set)
        fList.pack(side = LEFT, fill = BOTH)
        scrollbar.config(command = fList.yview)

        #Event handling method.
    def openFile(self):
            """Pops up an open file dialog, and if a file is selected
            displays its text in the text area and its pathname in the
            title bar."""

            fList = [("Python files", "*.py"),("Text files","*.txt")]
            fileName = tkinter.filedialog.askopenfilename(parent = self,
                                                      filetypes = fList)

            if fileName !="":
                file = open(fileName, 'r')
                text = file.read()
                file.close()
                self.outputArea.setText(text)
                self.setTitle(fileName)

    def saveFile(self, fileName=None):
            if self.__file == None:
                #save as new file
                self.__file =asksaveasfilename(intialfile="Untitled.txt",defaultextenstions=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])

            if self.__file=="":
                self.__file=None
            else:
                    #try to save the file
                file = open(self.__file,"w")
                file.write(self.__thisTextArea.get(1.0,END))
            
                #change the window title
            self.__root.title(os.path.basement(self.__file)+"-TextEditor")


    def newFile(self):
            self.__root.title("Untitled - TextEditor")
            self.__file = None
            self.__thisTextArea.delete(1.0,END)
            
                                                          
    def main():
        FileDialogDemo().mainloop()

    if __name__ == "__main__" :
        main()
        sys.exit()#exits system
Error:
Traceback (most recent call last): File "C:/Users/talka/Desktop/ITF100/HeatherMorgan_Unit4_Chapter8Project.txt", line 16, in <module> class FileDialogDemo(EasyFrame): File "C:/Users/talka/Desktop/ITF100/HeatherMorgan_Unit4_Chapter8Project.txt", line 86, in FileDialogDemo main() File "C:/Users/talka/Desktop/ITF100/HeatherMorgan_Unit4_Chapter8Project.txt", line 83, in main FileDialogDemo().mainloop() NameError: name 'FileDialogDemo' is not defined >>>
There are lots of errors in your code
    def main():
        FileDialogDemo().mainloop()
 
    if __name__ == "__main__" :
        main()
        sys.exit()#exits system
these are indented into class FileDialogDemo remove the indenting


self.__root.title(os.path.basement(self.__file)+"-TextEditor")
os has not been imported


fileName = tkinter.filedialog.askopenfilename(parent = self,
                                                      filetypes = fList)
filedialog has been imported directly so tkinter. is not required in front of it


rook = Tk()
scrollbar = Scrollbar(root)
typo rook instead of root


Lots of tkinter object used in the code without importing tkinter, add the following import
import tkinter as tk
and then add tk. in front of all the tkinter objects