+ 1
How to replace main.txt by name that is enter in entry?????
from tkinter import * root=Tk() f=open("main.txt","w+") replace=f.replace("main.txt","name.txt") f.close() name=StringVar() nam=Label(text="Name of customer") txtnam=Entry(textvariable=name) root.mainloop()
28 Answers
+ 4
I hate tkinter
+ 1
Do you want to rename the file?
+ 1
Module os has many method related for file handling.
+ 1
os has a method rename.
+ 1
Why it did not work?
+ 1
I created 2 programs in same dir:
helloworldprogram.py:
print("hi")
renamer.py:
import os
os.rename("helloworldprogram.py", "bananapeel.py")
And running renamer.py successfully changed "helloworldprogram.py" name to "bananapeel.py".
+ 1
Import os
os.rename("main.txt","name.txt")
☝️☝️ this is code that I have but it will creat name.txt but I want the name that is store inside name
+ 1
#Does this work?
import os
from tkinter import *
root=Tk()
f=open("main.txt","w+")
replace=f.replace("main.txt","name.txt")
f.close()
name=StringVar()
nam=Label(text="Name of customer")
txtnam=Entry(textvariable=name)
os.rename("main.txt", txtnam + ".txt")
root.mainloop()
+ 1
Did you try txtnam.get()?
+ 1
def rename():
os.rename("main.txt", txtnam.get())
Because the tkinter uses mainloop, you would need any tkinter widget (such as function) to call this function.
+ 1
Should I be able to type something if I took the file openings away?:
from tkinter import *
root=Tk()
name=StringVar()
nam=Label(text="Name of customer")
txtnam=Entry(textvariable=name)
root.mainloop()
+ 1
I couldn't type anything
+ 1
Aren't the widgets supposed to be packed?
+ 1
from tkinter import *
root = Tk()
name = StringVar()
nam = Label(text="Name of customer")
nam.pack()
txtnam = Entry(textvariable=name)
txtnam.pack()
root.mainloop()
+ 1
Sushmita Khadka I don't know what's grid, but I made a code that worked, it changed file main.txt name to text I typed + ".txt"
+ 1
Seb TheS Thank u so much your idea I mean your code
os.rename("main.txt" , txtnam+".txt") works I change a little bit...... but because of u I complete my project😊😊
Thank u
0
Yes rename this file and name should be same that I enter at entry
0
Already try but not working
0
Unsupportable operand type(s) for +: 'Entry and 'str' this kinds of error