-1

enter image description here

when i write in the execution's entry i want this to be saved in another text file
how ?
and this is my code

from tkinter import ttk
from tkinter import *

root = Tk()
label1=ttk.Label(root,text="Type your message : ",font="classic")
label1.pack()
entry1=ttk.Entry(root,width=70)
entry1.pack()
button=ttk.Button(root,text="Send",padding=7,cursor="hand2")
button.pack()
def Bclick () :
    entry1.delete(0,END)
    print("sent")
button.config(command=Bclick)
file = input("yo :")
root.mainloop()
with open('text','w') as myfile : 
    myfile.write(file)
  • It is not recommended to call console `input()` in a GUI application. What is the purpose of the line `file = input("yo :")` actually? You can use `entry1.get()` to get the input text in the entry box and save the input text to file since you already have code of saving file. – acw1668 Feb 03 '23 at 10:21
  • Welcome to Stack Overflow. "when i write in the execution's entry i want this to be saved in another text file" - I can't understand this, first off because "write in the execution's entry" doesn't make sense, and second because the code already will write whatever was provided for the `input`, and there is no other code that does anything with that input. – Karl Knechtel Feb 03 '23 at 10:48

2 Answers2

0

To get the value from an Entry widget, you need to use its get method, e.g.,

from tkinter import ttk
from tkinter import *

root = Tk()
label1 = ttk.Label(root, text="Type your message : ", font="classic")
label1.pack()
entry1 = ttk.Entry(root, width=70)
entry1.pack()
button = ttk.Button(root, text="Send", padding=7, cursor="hand2")
button.pack()

def Bclick():
    entryvalue = entry1.get()  # get what's in the Entry widget before clearing it
    entry1.delete(0,END)
    print("sent")

    # write the value to a file
    with open("text", "w") as myfile:
        myfile.write(entryvalue)  # write it to your file


button.config(command=Bclick)
#file = input("yo :")  # not sure what this was for, so I've commented it out

root.mainloop()

After this, your file text should contain whatever you entered into the Entry box.

Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32
  • 1
    Have you tested your code? When the file saving code is executed, `entryvalue` is undefined. You need to put the file saving code inside `Bclick()`. – acw1668 Feb 03 '23 at 10:34
  • You're right, I tested it with the `root.mainloop()` before the writing to file, then moved it in my answer. I've switched it back, but , as you suggest, it would be better to just put the saving code inside the `Bclick` function and avoid any use of globals. – Matt Pitkin Feb 03 '23 at 10:43
  • I've move the saving part into `Bclick`. – Matt Pitkin Feb 03 '23 at 10:44
  • Matt pitkin it works!! you're the bestt tyy – med amir Feb 03 '23 at 13:22
0

What your current code is doing is building the interface then it collects input through the terminal and writes it to a file, and then finally it displays the window to the user.

What you want to do instead is inside of your buttons callback method, collect the input from the Entry widget, and save whatever it contains in the your buttons callback.

from tkinter import ttk
from tkinter import *

filename = "MyTextFile.txt"

root = Tk()
label1=ttk.Label(root,text="Type your message : ",font="classic")
label1.pack()

entry1=ttk.Entry(root,width=70)
entry1.pack()

button=ttk.Button(root,text="Send",padding=7,cursor="hand2")
button.pack()

def Bclick():
    text = entry1.get()
    with open(filename, "wt") as fd:
        fd.write(text)
    entry1.delete(0,END)
    print("sent")

button.config(command=Bclick)

root.mainloop()
Alexander
  • 16,091
  • 5
  • 13
  • 29