Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Entry in textdocument
#1
Hello.
I've made two little script with tkinter.
In the first one, you have to enter an animal e.g. "dog" and the word dog, get's written into a textile called "new animals.txt".
Now in the second one, you have to enter an animal too. At this point, I have to problems: (Because I never did that before, and I don't know how to do it):

1. After you've entered the animal in the second script, you're clicking on a button. Now the animal get's written into a text file called "animals.txt". But I want the program to check, if the animal, also stands in "new animals.txt". If it is the case, it get's written into "animals.txt". If the animal, you've entered is not already in "newanimals.txt" not.

2. I've noticed, that if you enter an animal, e.g. dog and after that e.g. cat, the dog gets deleted and the cat is in the first line. How do I do it, that the cat, get's written into the second line, and the dog says in the first line?

Here's the part of my code, you need:

Animalcheck = StringVar()

Entry(window, textvariable=Animalcheck).grid(row=4, column=4)
ConfirmButton = Button(window, text="Confirm", command=animalcheck).grid(row=6, column=4)

def animalcheck():
        animalcheck = Animalcheck.get()
        saveanimal = open("animals.txt", "w")
        saveanimal.write(animalcheck)

That was an example of the second script. The first script looks like it, with a few changes in the document names.

Thanks for your help.

Piethon
Quote
#2
I do not do TKinter but if I had to venture a guess it is one of two things or both:

You open the file with open("animals.txt", "w") aka overwrite mode (I believe) try open("animals.txt", "a") or append mode

The .write() might overwrite as such what you need would be .append()

Note you will have to check the syntax of those two suggestions as again I do not do TKinter but it ought to have something like that.
silly American kah-nig-hit
Quote
#3
Sorry not working. I'm getting an error there, that there is no such thing as "append".

You usually use .append for lists, I think.
But this is not a list.
Quote
#4
Like I said you will have to check the syntax of those two suggestions as I do not do TKinter but it ought to have something like that

Aka consider what I posted as pseudo code - now using that pseudo code investigate how it is actually done in TKinter

For instance in some situations append() is put() instead then again maybe you use write() because instead of opening the file with "w" which might mean write or over-write you opened it with "a" which could mean append -- then again I cannot say that is within TKinter you will have to do the validation on your own as this was simply meant to point you in a possible valid direction it was totally up to you to figure out whether that direction headed out to the open sea or straight into the reefs. Now obviously it points close enough to the reefs to cause a shipwreck so make the necessary course corrections so that you make open sea.
silly American kah-nig-hit
Quote
#5
To append data to an existing file use open("filename", "a")
Quote
#6
(Sep-10-2019, 10:42 PM)Axel_Erfurt Wrote: To append data to an existing file use open("filename", "a")

Not working. Now my def animalcheck(): looks like this:

def animalcheck():
        animalcheck = Animalcheck.get()
        saveanimal = open("newanimal.txt", "a")
        savenanimal.append(namecheck)
        saveanimal.close()
This is the Error, I get:

AttributeError: '_io.TextIOWrapper' object has no attribute 'append'

Quote
#7
You misspell saveanimal in line 4 by adding an extra "n". Remove that and it works.
J
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 434 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:


Users browsing this thread: 1 Guest(s)