Page 1 of 1

Python programming question

Posted: Wed 18 Nov 2009, 04:43
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...

Posted: Wed 18 Nov 2009, 23:47
by ITAmember
The biggest thing I see is you should be using an object for your window.

Posted: Thu 19 Nov 2009, 03:59
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:

Python 3.1.1 with Tkinter and IDLE on Puppy linux 4.31

Posted: Wed 02 Nov 2011, 17:47
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/

Posted: Fri 11 Nov 2011, 18:00
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]