|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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). |
|
#4
|
||||
|
||||
|
Quote:
Why do you do that?? What about self.save_count = 1? Furthermore finally you should close a file. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Examine my code, please? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|