+ 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()

1st Jan 2020, 1:41 PM
Sushmita Khadka
Sushmita Khadka - avatar
29 Respostas
+ 4
I hate tkinter
1st Jan 2020, 3:07 PM
Seb TheS
Seb TheS - avatar
+ 1
Do you want to rename the file?
1st Jan 2020, 2:10 PM
Seb TheS
Seb TheS - avatar
+ 1
Module os has many method related for file handling.
1st Jan 2020, 2:14 PM
Seb TheS
Seb TheS - avatar
+ 1
os has a method rename.
1st Jan 2020, 2:14 PM
Seb TheS
Seb TheS - avatar
+ 1
Why it did not work?
1st Jan 2020, 2:16 PM
Seb TheS
Seb TheS - avatar
+ 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".
1st Jan 2020, 2:19 PM
Seb TheS
Seb TheS - avatar
+ 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
1st Jan 2020, 2:20 PM
Sushmita Khadka
Sushmita Khadka - avatar
+ 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()
1st Jan 2020, 2:22 PM
Seb TheS
Seb TheS - avatar
+ 1
Did you try txtnam.get()?
1st Jan 2020, 2:35 PM
Seb TheS
Seb TheS - avatar
+ 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.
1st Jan 2020, 2:45 PM
Seb TheS
Seb TheS - avatar
+ 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()
1st Jan 2020, 2:53 PM
Seb TheS
Seb TheS - avatar
+ 1
I couldn't type anything
1st Jan 2020, 2:55 PM
Seb TheS
Seb TheS - avatar
+ 1
Aren't the widgets supposed to be packed?
1st Jan 2020, 2:57 PM
Seb TheS
Seb TheS - avatar
+ 1
from tkinter import * root = Tk() name = StringVar() nam = Label(text="Name of customer") nam.pack() txtnam = Entry(textvariable=name) txtnam.pack() root.mainloop()
1st Jan 2020, 2:58 PM
Seb TheS
Seb TheS - avatar
1st Jan 2020, 3:05 PM
Seb TheS
Seb TheS - avatar
+ 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"
1st Jan 2020, 3:06 PM
Seb TheS
Seb TheS - avatar
+ 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
1st Jan 2020, 3:54 PM
Sushmita Khadka
Sushmita Khadka - avatar
0
Yes rename this file and name should be same that I enter at entry
1st Jan 2020, 2:13 PM
Sushmita Khadka
Sushmita Khadka - avatar
0
Already try but not working
1st Jan 2020, 2:15 PM
Sushmita Khadka
Sushmita Khadka - avatar
0
Unsupportable operand type(s) for +: 'Entry and 'str' this kinds of error
1st Jan 2020, 2:28 PM
Sushmita Khadka
Sushmita Khadka - avatar