Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Can you help me to understand the "asksaveasfile" and "askopenfilename" functions?
#1
hi guys,

can you give me a simple example on how to use correctly the "filedialog.askopenfilename ()" and "filedialog.asksaveasfile ()" functions?

my GUI software should follow these steps:

1. the user takes a ".txt" file browsing on his local disk via a button widget called "browse" (the complete path will be displayed also in a entry widget near to the button).
2. the user clicks on another button widget called "export".
3. a part of my code, already written, processes the input file and return a list.
4. another part of my code, creates a new excel file and compiles it with all information contained in the list of the point 2. to do it I used the xlsxwriter library.
5. when the excel file has been written, a new window appears (it will help the user to save the excel file in his desired location)

can you give me a simple exaple to help me to reach my goal?
Quote
#2
Please post your code and any errors that you are receiving when run. It is necessary to show what you have done and what you have tried along with errors to better allow members to offer assistance.
"Often stumped... But never defeated."
Quote
#3
(May-21-2020, 07:53 PM)DT2000 Wrote: Please post your code and any errors that you are receiving when run. It is necessary to show what you have done and what you have tried along with errors to better allow members to offer assistance.

yeah, you are right. I wrote a little example (the two input files are text files with just a word in the first line). below my code:

#!/usr/bin/python3

from tkinter import *
from tkinter import ttk, filedialog
import xlsxwriter as xw

class MainWindow:
    
    def __init__(self, parent):
        # sutom styles for "ttk" widgets:
        style = ttk.Style()
        style.configure("Main.TLabel", foreground="#99c1e8")
        style.configure("Frame.TButton", background="#ffffff", padding = 0)
        style.configure("TButton", padding = 0)        
        # main window attributes:    
        self.parent = parent
        self.parent.title("My Software - Example")
        self.parent.geometry("530x532+360+200")
        self.parent.resizable (width=False, height=False)
        self.parent.configure(background="#f0f0f0")
        # main windows widgets:
        frame = Frame(self.parent, background = "#ffffff", width = 506, height = 475, highlightbackground = "#d3d5d9", highlightthickness = 1)
        frame.place(x = 12, y = 12)
        ttk.Button(self.parent, text = "Export", width = 16, command = self.__Save).place(x = 334, y = 497)
        # two "input buttons" with two entries:
        ttk.Label(frame, text = "Text file 1:", background = "#ffffff").place(x = 16, y = 114)  
        self.first_word = ttk.Entry(frame, text="", width=51)
        self.first_word.place(x = 122, y = 114)
        ttk.Button(frame, text="...", width = 4, style = "Frame.TButton", command = self.__GetInput1).place(x = 440, y = 113)
        #
        ttk.Label(frame, text = "Text file 2:", background = "#ffffff").place(x = 16, y = 145)  
        self.second_word = ttk.Entry(frame, text="", width=51)
        self.second_word.place(x = 122, y = 145)
        ttk.Button(frame, text="...", width = 4, style = "Frame.TButton", command = self.__GetInput2).place(x = 440, y = 144)        

        
    def __Save(self):
        # this code create a file excel named "final_document.xlsx" in the same directory where I run the script. how can I integrate this kind of code with "filedialog.asksaveasfile()" function? what I want is first creating the excel file in RAM (without give a specific location) and then, save it where I want and with the name that I want, using "filedialog.asksaveasfile()" function. the save process must be done via GUI! how can I reach my goal?
        #
        # wb = xw.Workbook("final_document.xlsx")
        # ws = wb.add_worksheet("RESULT")
        # ws.write(0, 0, self.first_word)
        # ws.write(0, 1, self.second_word)
        # wb.close()
        #
        #
        files = [('Excel Document', '.xlsx'), ('All Files', '*.*')]
        file = filedialog.asksaveasfile(filetypes = files, defaultextension = files) 

    # is there a way to use just one "GetFunction"?
    def __GetInput1(self):
        path = filedialog.askopenfilename()
        self.first_word.delete(-1, END)
        self.first_word.insert(0, path)
        
    def __GetInput2(self):
        path = filedialog.askopenfilename()
        self.second_word.delete(-1, END)
        self.second_word.insert(0, path)

def main():
    root = Tk()
    app = MainWindow(root)
    root.mainloop()

if __name__ == "__main__":
    main()
you can read my dubts about this code in my two last comments. I can get the info from my first two button but I don't understand how to proceed to save the new information in a file excel using the third button ("Export"). can you help me to fix this code?
Quote
#4
Here is a link with example
https://stonesoupprogramming.com/2018/03...le-dialog/
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
Johnny's CookBook - Powered By: Python3.8 | Bootstrap4 | Apache2 | Django
Quote
#5
(May-21-2020, 10:59 PM)menator01 Wrote: Here is a link with example
https://stonesoupprogramming.com/2018/03...le-dialog/

the examples are very clear in that web site, but it doesn't help me to reach my goal. I already explained what I want to do in my last reply:

    def __Save(self):
        # this code creates a file excel named "final_document.xlsx" in the same directory where I run the script.
        # how can I integrate this kind of code with "filedialog.asksaveasfile()" function? what I want is first
        # creating the excel file in RAM (without give a specific location) and then, save it where I want and with 
        # the name that I want, using "filedialog.asksaveasfile()" function. the save process must be done via GUI!  
        # how can I reach my goal?
        #
        # wb = xw.Workbook("final_document.xlsx")
        # ws = wb.add_worksheet("RESULT")
        # ws.write(0, 0, self.first_word)
        # ws.write(0, 1, self.second_word)
        # wb.close()
        #

I understood the "askopenfilename" function but I still don't understand how to save a file already created by my code. my goal is take two or more files with "askopenfilename" function and use a button widget to activate my code (read the input files and create a new file, in my example a file excel). when this process is completed, somthing like "asksaveasfile" function has to ask to the user where to save the new file. from what I understood "asksaveasfile" returns a new file in write mode, but it's not what I want to do. what I want to do is very similar to when you write a word document. when you finished to write, you have to save the document in a directory, and Word asks to you "where do you wanna save your document just written?"
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 565 Dec-29-2019, 08:12 PM
Last Post: JackMack118

Forum Jump:


Users browsing this thread: 1 Guest(s)