-3

I have made a program tht reads the stored file details. I want to create a .txt file using python tkinter .the name of the creating file will be given by user in entry box.

Can you provide the correct code.please

user5849405
  • 11
  • 1
  • 1

2 Answers2

1
from tkinter import *
root = Tk()

def clicked():
    Input = entry1.get()
    FileName = str("filepath" + Input + ".txt")
    TextFile = open(FileName,"w")

entry1 = Entry(root)
button1 = Button(root,text="Press to create text file", command = clicked)
entry1.pack()
button1.pack()

root.mainloop()

This will make a function that runs when the button is clicked, it will get the text in the entry box then make a file path, finally it will attempt to open this file, as the file doesn't exist it will instead create a new file under this name.

LuGibs
  • 190
  • 1
  • 1
  • 14
0

Simple example

import tkinter as tk

def write_file():
    name = e.get() # get text from entry
    with open(name + ".txt", "w") as f: # open file 
        f.write("Hello World\n") # write doesn't add '\n'
    # `with` will close file automatically

root = tk.Tk()

e = tk.Entry(root)
e.pack() # can't be `e = Widget(...).pack()`

tk.Button(root, text="Save", command=write_file).pack()

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148