Python programming question

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
moogsydodong
Posts: 81
Joined: Mon 16 Mar 2009, 10:35
Location: Tagum City, Davao del Norte, Mindanao, Philippines

Python programming question

#1 Post by moogsydodong »

hi all...

I'm SLOWLY learning python and want to learn more from you guys who knows the language...

my first experiment is this...

Code: Select all

#!/usr/bin/env python


from Tkinter import *
from ScrolledText import *
from tkFileDialog import *


def opnfile():
	
	filename = askopenfilename(filetypes=[("allfiles","*.py"),("pythonfiles","*.py")])
	file = open(filename, 'r')
	stext.delete("1.0", END)
	stext.insert("1.0",file.read())
	thisfile = filename
	file.close()
def savfile():
	
	filename = asksaveasfilename(filetypes=[("allfiles","*.py"),("pythonfiles","*.py")])
	thisfile = filename
	file = open(thisfile, 'w')
	file.write(stext.get("1.0", END))
	file.close()

root = Tk()


# create a menu
menu = Menu(root)
root.config(menu=menu)
stext = ScrolledText(root)
stext.pack()

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open", command=opnfile)
filemenu.add_command(label="Save...", command=savfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=menu.quit)


mainloop()

just a simple text editor...

any comments will be appreciated...
Last edited by moogsydodong on Thu 19 Nov 2009, 03:51, edited 1 time in total.
...SHUT UP AND LISTEN!!!...In a battle there is only ONE COMMANDER!!!

ITAmember
Posts: 167
Joined: Sun 13 Sep 2009, 18:50
Location: The middle of a cornfield

#2 Post by ITAmember »

The biggest thing I see is you should be using an object for your window.

User avatar
moogsydodong
Posts: 81
Joined: Mon 16 Mar 2009, 10:35
Location: Tagum City, Davao del Norte, Mindanao, Philippines

#3 Post by moogsydodong »

thanks ITAmember...

like I said I am new to this and learning Python SLOWLY...

I haven't made it to the classes part of Python I don't know anything about objects and ObjectOriented approach yet...anyway Ive got Ideas on this text editor from here

the problem is the window in this editor is flickering...I don't know why...

well...back to learning python...SLOWLY... :wink:
...SHUT UP AND LISTEN!!!...In a battle there is only ONE COMMANDER!!!

hailpuppy
Posts: 73
Joined: Wed 28 Oct 2009, 07:49

Python 3.1.1 with Tkinter and IDLE on Puppy linux 4.31

#4 Post by hailpuppy »

Get python 3.1.1 with tkinter and IDLE on below URL.Thanks a lot ITAmember for the great job!!!


http://dotpups.de/ITAmember/python/

jamesjeffries2
Posts: 196
Joined: Mon 28 Apr 2008, 00:50

#5 Post by jamesjeffries2 »

Quick tip about running python files. You probably want your 'main' (the bit you want to run that is outside of functions) to have

Code: Select all

#..your functions go here..

if __name__ == "__main__":
  root = Tk()
  #... the rest of your code under here...
So your functions are at the top with your main separate at the bottom. The if statement makes sure that the code it contains is only run when you run your python file. Not when it is imported by another file (you'll learn more about that later I'm sure ;) )[/code]

Post Reply