Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI
#1
Getting a class FileDialogDemo not found in this code. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""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
Reply
#2
Please use proper tags and post any errors.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""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 >>>
Reply
#4
There are lots of errors in your code
1
2
3
4
5
6
def main():
    FileDialogDemo().mainloop()
  
if __name__ == "__main__" :
    main()
    sys.exit()#exits system
these are indented into class FileDialogDemo remove the indenting


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


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


1
2
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
1
import tkinter as tk
and then add tk. in front of all the tkinter objects
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020