-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

6
  • 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 Commented Jul 23, 2021 at 8: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) Commented Jul 23, 2021 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 Commented Jul 23, 2021 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. I think this will solve your issue. Commented Jul 23, 2021 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 Commented Jul 23, 2021 at 13:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.