[Tutor] How to write lines in a text file (GUI)

bodsda at googlemail.com bodsda at googlemail.com
Tue Nov 29 19:27:39 CET 2011


This is a very simple objective to achieve, so instead of giving you a straight up solution, I will just nudge your train of thought in the right direction.

Note: I'm struggling to follow your code whilst reading on my phone so the following suggestions make no reference to your particular codebase

Let's first assume that all buttons could be pressed in any order, therefore given the following presses: but1, but2, but2, but3, we will have the first line with hi_1, the second line empty and the third line hi_3. Now I also assume that there is no 'update file' button, therefore each press of a button needs to update the file making a change to a single line. That sounds to me like a good use for an update_file function that accepts a parameter of which line should be updated. So the main challenge is to write a function that can read/write to a file certain data depending on the argument passed. The function should:

Read file
Identify line to be changed
If line empty
Change line to hi_#
Write file

When you open a file, you could then use the read function to read the whole file, or you could use the readlines function to read the file line by line. Experiment with both, see what data types they both return and decide which is most suitable. Bear in mind you need to easily differentiate each line and access them individually.

If your still struggling to write this function, show us how far you get and we can go from there.

Hope this helps,
Sent from my BlackBerry® wireless device

-----Original Message-----
From: "Mic" <o0MB0o at hotmail.se>
Sender: tutor-bounces+bodsda=googlemail.com at python.org
Date: Tue, 29 Nov 2011 19:02:52 
To: <tutor at python.org>
Subject: [Tutor] How to write lines in a text file (GUI)

Hey again.

I figured I would first post this piece of code and then ask my questions.

FREE = "green"
OCCUPIED = "red"

class Mainwindow(Frame):
     def __init__(self,master):

     def create_mainwidgets(self):
         self.klicka=Button(self, text="Press the button",

     def uppdatera(self):

class SeatButton(tk.Button):
     def __init__(self, master, index):
         text = str(index+1)
         super(SeatButton, self).__init__(master,
                                          text=text, bg=FREE,
         self.filename = "Germany_France{}.txt".format(index+1)
         self.occupied = False
         if os.path.exists(self.filename):

     def clicked(self):
         self.occupied = not self.occupied
         if self.occupied:
             self["bg"] = OCCUPIED
             self["bg"] = FREE

class SeatWindow(tk.Toplevel):
      def __init__(self, master):
          super (SeatWindow, self).__init__(master)

      def create_widgets(self):
          for index in range(20):
              button = SeatButton(self, index)
              row, column = divmod(index, 4)
              button.grid(row=row, column=column)


I am now writing another program. It turned out just fine, but I have one

If button one is pressed I want the program to write, say hi_1, on the first
line in a text file.
If button one is pressed again, the first line should be empty.

If button two is pressed I want the program to write hi_2 in the second line
in the text file.
If button two is pressed again, the second line in the text file should be

If button three is pressed I want the program to write hi_3 in the third
line in the text file.
if button three is pressed again, the third line in the text file should be

There shouldn't be any other changes to the program other than this above.
There is already a text file
created, the program doesn't need to create one. We can call that file "Hi".

I hope you understand what I want to do here. I have tried the entire
evening yesterday to get this to work, I tried using writeline() but I can't
get it to work.
What are your suggestions to this?

Thank you for your help and time!

Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:

More information about the Tutor mailing list