GUI - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: GUI (/thread-28484.html) |
GUI - talkativemom - Jul-21-2020 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 RE: GUI - menator01 - Jul-21-2020 Please use proper tags and post any errors. RE: GUI - talkativemom - Jul-21-2020 """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
RE: GUI - Yoriz - Jul-21-2020 There are lots of errors in your code def main(): FileDialogDemo().mainloop() if __name__ == "__main__" : main() sys.exit()#exits systemthese are indented into class FileDialogDemo remove the indentingself.__root.title(os.path.basement(self.__file)+"-TextEditor") os has not been importedfileName = tkinter.filedialog.askopenfilename(parent = self, filetypes = fList) filedialog has been imported directly so tkinter. is not required in front of itrook = 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 tkand then add tk. in front of all the tkinter objects
|