-2

so,my question is How to make a custom file extension for your Tkinter program python i also searched in on youtube but the program ask from which program you want to open the file and I don't want it can you give me the answer to that

  • 1
    What do you mean by _"custom file extension for your Tkinter program python"_ Can you please explain clearly and if possible then with example also – imxitiz Jul 23 '21 at 08:05
  • so I am guessing that you are trying to open the python file without the interpreter, so basically, you go to the file explorer and then double click on your `.py` file, correct? well that won't work (unless you specify how to open it somehow by default and stuff), well you otherwise have two options: open the file using `cmd` and then type: `python file_name.py` or convert your file to an executable (`.exe`) file and then it will run as any other executable file on windows (or the according file depending on your OS) – Matiiss Jul 23 '21 at 12:18
  • @Xitiz by custom file extension i mean like when you make a file in notepad it is saved as file_name.txt here by file extension I mean the .txt here are some more examples :.docx,.pd,f.py,so clearly I want to make a file extension in python – Muhammad Jamil Jul 23 '21 at 13:08
  • If you just want to make the extension, then there is no big deal, you can create `.xyz`,`.digital` preety much anything but if you want to open that extension(file) with some specific program. The that is big deal. You may look [here](https://stackoverflow.com/questions/41639740). I think this will solve your issue. – imxitiz Jul 23 '21 at 13:25
  • @Xitiz as you said if you just want to create an extension you can make pretty much anything can you help me making one that would be pretty helpful – Muhammad Jamil Jul 23 '21 at 13:34
  • @Xitiz yes i am using tkinter – Muhammad Jamil Jul 23 '21 at 13:36

1 Answers1

0

You can try this:

from tkinter.filedialog import asksaveasfile
from tkinter import *

base = Tk()

def SaveFile():
   data = [('All tyes(*.*)', '*.*'),(<Show it to user>,<actual extension>)]
   file = asksaveasfilename(filetypes = data, defaultextension = data)
   # file will have file name provided by user.
   # Now we can use this file name to save file.
   with open(file,"w") as f:
      f.write(<data you want to save>)

save_btn = Button(base, text = 'Click to save file ', command = SaveFile)
save_btn.pack(side = TOP, pady = 20,padx = 50)

base.mainloop()

In file you can write preety much anything. .digital, .kid, .anything.

imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • I strongly advise against using wildcard (`*`) when importing something, You should either import what You need, e.g. `from module import Class1, func_1, var_2` and so on or import the whole module: `import module` then You can also use an alias: `import module as md` or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue. – Matiiss Jul 23 '21 at 13:52