July 28th, 2004, 06:03 PM
Examine my code, please?
Hi, i've just started learning Python, and have written a simple notepad like program. I would like some expert to just take a quick look at my code and see if it is all good - I don't want to be developing any bad habbits this soon.
Well that's a screen length, but just barely... I'll .zip it if needed.
# Notepad 2
# Acts like Notepad
from Tkinter import *
""" GUI application which counts button clicks. """
def __init__(self, master):
""" Initialize the frame. """
self.save_count = int("1")
""" 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)
""" 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)
""" update the button count on click """
contents = self.user_txt.get(0.0, END)
self.new_file = open("test.txt", "w")
self.lbl["text"] = "File Saved", str(self.save_count)
self.save_count += 1
print "File saved."
root = Tk()
app = Application(root)
Oh, and, quick question - how can I close the GUI/script with a button? I have tried some things but they don't work.
July 28th, 2004, 07:44 PM
Keep at it, it's worthy
I would normally redirect the WM_DELETE_WINDOW to my own handler - this allows the program to tidy up when the user tries to pull the rug out.
Within that function you would eventually call:
which actually does the window clean up. You can of course assign this function to a button.
WRT your code - why not include root in the application class?
There is a ScrolledText widget that is pretty effective - does every thing Text does but has a scroll bar. You can
If you are planning to display more than just ASCII then it is a good idea to set up the Text widget spacing(1..3) options to 1 or 2 pixels. This prevents the top and bottom of some accented characters from being clipped. The wrap option may be right for you.
myTextBox = ScrolledText.ScrolledText( ....every thing as Text widget...)
As a personal preference; for programs more than a few lines I don't do
from Tkinter import *
and use the standard
This just stops me interfering with already defined names - Tkinter has a lot of them! It makes the code a little more wordy but IMO easier to check.
Last edited by Grim Archon; July 28th, 2004 at 07:48 PM.
July 28th, 2004, 09:12 PM
Thanks for the advice! I will try it out.
Since i'm new, i'm just doing it the way they did in the book (Python Programming for the absolute beginner).
August 12th, 2004, 07:36 AM
Why do you do that??
Originally Posted by Possibility
What about self.save_count = 1?
Furthermore finally you should close a file.