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


Messages In This Thread
cancelling open dialog gives empty string - by rwahdan - Jul-01-2021, 01:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How a QMainWindow can open a dialog? panoss 4 3,536 Feb-03-2022, 04:33 PM
Last Post: panoss
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,234 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  [WxPython] Return code when closing the dialog ioprst 1 3,181 Aug-13-2019, 11:47 AM
Last Post: jefsummers
  PyQT5 : Unable to Create Another Dialog While One is Open iMuny 3 3,881 Jul-17-2019, 11:40 AM
Last Post: iMuny
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 4,226 Apr-03-2019, 06:33 PM
Last Post: Yoriz
  [WxPython] how to run the dialog from another py file royer14 0 2,693 Jul-02-2018, 05:33 AM
Last Post: royer14
  FileBrowser dialog reverendfuzzy 5 4,638 May-06-2018, 01:56 PM
Last Post: reverendfuzzy
  I'm trying to create a simple yes/no dialog box RedSkeleton007 15 18,537 Apr-25-2018, 05:10 PM
Last Post: nilamo
  About Dialog in PyQt5 on macOS cpuin 0 2,878 Jan-23-2018, 08:50 PM
Last Post: cpuin
  set font from dialog box pyside python Gigux 1 3,599 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