[Tutor] .insert(0.0, whole_thing) problem
Beowolf
beowolf at the-barracks.net
Thu Jul 29 00:14:16 CEST 2004
Hi, I just started learning Python :D
I'm making a simple script with a GUI, reading from a text file, but the
problem is that the contents won't insert into the text area, though
they do print ok in the console window. Here is my code, can you see
what I am doing wrong? (in the def open_file(self) section)
# Notepad 2
# Acts like Notepad
# Nick Bakewell - 7/28/04
from Tkinter import *
class Application(Frame):
""" GUI application which counts button clicks. """
def __init__(self, master):
""" Initialize the frame. """
Frame.__init__(self, master)
self.grid()
self.create_widget()
self.open_file()
self.save_count = int("1")
def create_widget(self):
""" create the button and status """
self.lbl = Label(self, text = "Unsaved")
self.lbl.grid(row = 0, col = 0)
self.bttn = Button(self)
self.bttn["text"] = "Save File"
self.bttn["command"] = self.save_file
self.bttn.grid(row = 0, col = 1)
self.lbl2 = Label(self, text = "Editing file 'test.txt'")
self.lbl2.grid(row = 3, col = 0, columnspan = 2)
def open_file(self):
""" open the file for writing and reading """
self.text_file = open("test.txt", "r")
self.whole_thing = self.text_file.read()
self.user_txt = Text(self, width = 50, height = 30, wrap = WORD)
self.user_txt.grid(row = 2, col = 0, columnspan = 2, sticky = W)
self.user_txt.delete(0.0, END)
self.user_txt.insert(0.0, self.whole_thing)
print self.whole_thing
def save_file(self):
""" update the button count on click """
contents = self.user_txt.get(0.0, END)
self.new_file = open("test.txt", "w")
self.new_file.write(contents)
self.lbl["text"] = "File Saved", str(self.save_count)
self.save_count += 1
print "File saved."
# main
root = Tk()
root.title("Button Test")
root.geometry("400x500")
app = Application(root)
root.mainloop()
More information about the Tutor
mailing list