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
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 433 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  [PyQt] Received RunTimeError after script concludes, closing Dialog Window (clicking x-out) skipper0802 0 1,098 Oct-09-2020, 09:23 PM
Last Post: skipper0802
  [WxPython] Return code when closing the dialog ioprst 1 1,720 Aug-13-2019, 11:47 AM
Last Post: jefsummers
  PyQT5 : Unable to Create Another Dialog While One is Open iMuny 3 1,677 Jul-17-2019, 11:40 AM
Last Post: iMuny
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 2,415 Apr-03-2019, 06:33 PM
Last Post: Yoriz
  [WxPython] how to run the dialog from another py file royer14 0 1,680 Jul-02-2018, 05:33 AM
Last Post: royer14
  FileBrowser dialog reverendfuzzy 5 2,712 May-06-2018, 01:56 PM
Last Post: reverendfuzzy
  I'm trying to create a simple yes/no dialog box RedSkeleton007 15 12,386 Apr-25-2018, 05:10 PM
Last Post: nilamo
  About Dialog in PyQt5 on macOS cpuin 0 1,827 Jan-23-2018, 08:50 PM
Last Post: cpuin
  set font from dialog box pyside python Gigux 1 2,450 May-08-2017, 12:25 AM
Last Post: Joseph_f2

Forum Jump:

User Panel Messages

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