Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 28th, 2004, 06:03 PM
Possibility Possibility is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 53 Possibility User rank is Corporal (100 - 500 Reputation Level)Possibility User rank is Corporal (100 - 500 Reputation Level)Possibility User rank is Corporal (100 - 500 Reputation Level)Possibility User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 h 4 m 10 sec
Reputation Power: 6
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.

Code:
# Notepad 2
# Acts like Notepad
# 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()


Well that's a screen length, but just barely... I'll .zip it if needed.

Oh, and, quick question - how can I close the GUI/script with a button? I have tried some things but they don't work.

Thanks!

Reply With Quote
  #2  
Old July 28th, 2004, 07:44 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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.

root.protocol('WM_DELETE_WINDOW',deleteWindow)

Within that function you would eventually call:
Code:
def deleteWindow():
    .....
    root.destroy() 

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
Code:
import ScrolledText
myTextBox = ScrolledText.ScrolledText( ....every thing as Text widget...)

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.

As a personal preference; for programs more than a few lines I don't do
from Tkinter import *
and use the standard
import Tkinter

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.

Good luck,
grim
__________________
*** Experimental Python Markup CGI V2 ***

Last edited by Grim Archon : July 28th, 2004 at 07:48 PM.

Reply With Quote
  #3  
Old July 28th, 2004, 09:12 PM
Possibility Possibility is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 53 Possibility User rank is Corporal (100 - 500 Reputation Level)Possibility User rank is Corporal (100 - 500 Reputation Level)Possibility User rank is Corporal (100 - 500 Reputation Level)Possibility User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 h 4 m 10 sec
Reputation Power: 6
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).

Reply With Quote
  #4  
Old August 12th, 2004, 07:36 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 6
Quote:
Originally Posted by Possibility
Code:
...
     self.save_count = int("1")
....



Why do you do that??

What about self.save_count = 1?
Furthermore finally you should close a file.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Examine my code, please?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT