Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with this problem using Python programming, thank you! Write a GUI-based program that implements an image browser for your computers file system. The

Need help with this problem using Python programming, thank you!

Write a GUI-based program that implements an image browser for your computers file system. The look, feel, and program logic should be like those of the simple text file browser developed in this chapter. The file dialog should filter for GIF image files, and create and open a PhotoImage when a file is accessed.

10:14 59% File Dialogs As anyone who has opened or saved a file on a modern computer knows, GUI-based programs allow the user to browse the computer's file sys- tem with file dialogs. Figure 8-17 shows a file dia- log asking for an input file on my computer. Figure 8-17 g De h8.den S Aoplionen ge 431 Mac 30 41 um bparine vied 2 Py A file dialog Python's tkinter.filedialog module includes two, functions, askopenfilename and asksaveasfile- name, to support file access in a GUI-based program. Each function pops up the standard file dialog for the user's particular computer system. If the user selects the dialog's Cancel button, the function returns the empty string. Otherwise, when the user selects the Open or Save button, the function returns the full pathname of the file that the user has selected (opening or saving) or 19 mintes 25 pages left 10:14O 59% Astring representing the directory in which to open the dialog initialdir Astring representing.the filename to display in the save dialog name field initialfile The dialag's parent window remt Astring to display in the dialog's title bar title You can use a file dialog and a text area to cre ate a simple browser that allows the user to view text files. As shown in Figure 8-18, when the user clicks the Open button and chooses a file from the file dialog, the text of the file is input and dis- played in the window's text area. Figure 8-18. JUsaranbekReed 2 Python BookChaptah 0Sudant Fedgderopy Fle Filedal dron breyythong ipers teyfr imert tkinter.ftedielo cless Filalag ) moro the sse of o fite dislog. dini( nd widets e.cwi sea sel.outputrea-elf.oddTexrea r colame- with ,eight5 Open 17 minutes 23 pages left 10:14 59% entered as input (saving only) in the dialog. The program can then use the filename to open the file for input or output in the usual manner. For purposes of this book, we use the following syntax with these two functions: fList Eython files". "py). CText files", "tt filenane tkinter.Filedialog.askopenfilename (parent self filetypes List) filenane tkinter. filedial og. asksaveasfilenase(parent self Note that you can use the optional filetypes ar- gument to mask the types of files available for input. In our example, we want the user to be able to open files with a py or txt extension, and no others. Table 8-4 lists all of the optional argu- ments one can supply to the two file dialog functions. Table 8-4 File Dialog Methods The Optional Arguments to the Value Argument The extension to add to the filename, if not given by the user lignored by the open dialog defaultex tension Assquence of label pattern) tuples Specifies the file types available for fletypes input 18 mindes 24 pages left Snapchat 59% from robertsi ww fre bre iert tsyr prt tkinterfiedialog class Fioal )I rnes the se ofa file disteg iniss the wind and wderts, tsranint.(se,le Dalng m e.cutputreself.derea calae- wteth-, hetght 15 Open A simple file browser Here is the code for the window class: fros breeaypythong epart EasyFrase aport tkinter.fiedialog class FileDialogeno(Easyframe): Demonstrates the use of a ile dialog. def init (sel) ets up the window and widgets. Easyframe init (self. File Dialog Dene self.eutpstArea self.addTextArea rw-o celu width 80 beight 15) selfaddatton(text pen", row-, calu-0, command self.openFile) Event handl ing sethod def openFile(self ps up an open file dialoa, and if a file is selected, displays its text in the test area and its pathnane in the title bar." fList [Python Files,py CText filese_txt ") fileanetkinter. filedialog. askopenfilename (parent self ir rease le opeileane, Filetypes fLise text file.reado ile.close self.putes.setText(text) self.setTitleileane) 17 minutes 23 pages left

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Core Ios Developer S Cookbook Core Recipes For Programmers

Authors: Erica Sadun ,Rich Wardwell

5th Edition

0321948106, 978-0321948106

More Books

Students also viewed these Programming questions