Python Forum
[Tkinter] cancelling open dialog gives empty string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] cancelling open dialog gives empty string
#1
Hi,

I am trying to open a file using the file dialog but what if the user decides to press cancel? a new line is added to my text widget. I don't want that extra line added there, how to achieve that?

text_file = filedialog.askopenfilename(initialdir="C:/Documents", title="Open File",
        filetypes=(("Text Files", "*.txt"),("HTML Files", "*.html"),
                   ("Python Files", "*.py"), ("All Files","*.*")))

    if (len(text_file) !=0):
        my_text.delete("1.0", END)
        open_status_name = text_file
        name, file_extension = os.path.splitext(text_file)
        name = name.split('/')[-1].split('.')[0]
        root.title(name+file_extension)
        status_bar.config(text = "File Opened Successfully!        ")
        text_file = open(text_file,"r")
        stuff = text_file.read()
        my_text.insert(END, stuff)
        text_file.close()
   else:
what i need to do in the else statement to delete that extra line because the user cancelled the operation?

Thanks
Reply
#2
Prefix your question with what GUI you are using. Better yet, make your example code complete so others can run it.

I don't see where you are adding anything to a text widget if text_file is empty.
Reply
#3
Assuming this code is in a function (it probably should be), then;

if text_file = "":
    return
after the filedialog line.


(Jul-01-2021, 01:04 PM)rwahdan Wrote: Hi,

I am trying to open a file using the file dialog but what if the user decides to press cancel? a new line is added to my text widget. I don't want that extra line added there, how to achieve that?

text_file = filedialog.askopenfilename(initialdir="C:/Documents", title="Open File",
        filetypes=(("Text Files", "*.txt"),("HTML Files", "*.html"),
                   ("Python Files", "*.py"), ("All Files","*.*")))

    if (len(text_file) !=0):
        my_text.delete("1.0", END)
        open_status_name = text_file
        name, file_extension = os.path.splitext(text_file)
        name = name.split('/')[-1].split('.')[0]
        root.title(name+file_extension)
        status_bar.config(text = "File Opened Successfully!        ")
        text_file = open(text_file,"r")
        stuff = text_file.read()
        my_text.insert(END, stuff)
        text_file.close()
   else:
what i need to do in the else statement to delete that extra line because the user cancelled the operation?

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there is a printer dialog box in PySimpleGUI shaunfish 2 1,583 Aug-22-2022, 07:29 PM
Last Post: deanhystad
  How a QMainWindow can open a dialog? panoss 4 3,480 Feb-03-2022, 04:33 PM
Last Post: panoss
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,206 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  [WxPython] Return code when closing the dialog ioprst 1 3,156 Aug-13-2019, 11:47 AM
Last Post: jefsummers
  PyQT5 : Unable to Create Another Dialog While One is Open iMuny 3 3,817 Jul-17-2019, 11:40 AM
Last Post: iMuny
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 4,181 Apr-03-2019, 06:33 PM
Last Post: Yoriz
  [WxPython] how to run the dialog from another py file royer14 0 2,676 Jul-02-2018, 05:33 AM
Last Post: royer14
  FileBrowser dialog reverendfuzzy 5 4,600 May-06-2018, 01:56 PM
Last Post: reverendfuzzy
  I'm trying to create a simple yes/no dialog box RedSkeleton007 15 18,405 Apr-25-2018, 05:10 PM
Last Post: nilamo
  About Dialog in PyQt5 on macOS cpuin 0 2,859 Jan-23-2018, 08:50 PM
Last Post: cpuin

Forum Jump:

User Panel Messages

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