Reputation: 214
I’ve many question about python class and tkinter. I never used before but I’m trying to make my first program and link it to a gui. I did my programs and my gui separately, it’s a bad choice?
My first part of code is :
from datetime import datetime
class MyProgram:
#dictonary with the information
d={1: ('sun','Paris',datetime(2012,4,17,00,00)), 2: ('cloud','Londres',datetime(2012,6,24,00,00)), 3 : ('snow','NewYork',datetime(2012,8,8,00,00)),4 : ('sun','Tokyo',datetime(2012,11,30,00,00))}
#List of string of d
d_string=[('sun','Paris',str(datetime(2012,4,17,00,00))),('cloud','Londres',str(datetime(2012,6,24,00,00))),('snow','NewYork',str(datetime(2012,8,8,00,00))),('sun','Tokyo',str(datetime(2012,11,30,00,00)))]
#function to query d
def queryD(timeAfter=datetime(1900,01,01,00,00), timeBefore=datetime(2500,01,01,00,00),place=None):
if place == None:
answer = [[key,values] for key,values in MyProgram.d.items() if values[2]>timeAfter and values[2]<timeBefore]
else:
answer = [[key,values] for key,values in MyProgram.d.items() if values[2]>timeAfter and values[2]<timeBefore and values[1]==place]
return answer
#function to write the results of queryD not finished yet because i can just print d_string, but it is not the queastion
def writeIndex():
#open index.txt and give the right to write
myFile=open('output.txt', 'w')
for l in MyProgram.d_string:
myFile.writelines('\t'.join(l)+'\n')
myFile.close()
return myFile
#function to read the file output
def readIndex():
#open index.txt and give the right to read
with open('output.txt', 'r') as f:
#return all the written informations in index.txt in a terminal
return [myIndex.split('\t') for myIndex in f.readlines()]
My GUI is :
from Tkinter import *
from datetime import *
import MyProgram
class App:
def __init__(self, gui):
gui = Frame(gui)
gui.grid()
#AFTER
#Create Text
self.textAfter = Label(gui, text="Put a date : ")
#give to Text a place
self.textAfter.grid(column=0,row=0)
#Create an area to write text
self.entryAfter = Entry(gui)
self.entryAfter.grid(column=1,row=0,sticky='EW')
self.entryAfter.insert(0, 'YYYY/MM/DD')
self.entryAfter.focus_set()
#Create a button
self.buttonAfter = Button(gui,text=u'After', command=self.getAfterTxT)
self.buttonAfter.grid(column=2,row=0)
#BEFORE
self.textBefore = Label(gui, text="Put a date : ")
self.textBefore.grid(column=0,row=1)
self.entryBefore = Entry(gui)
self.entryBefore.grid(column=1,row=1,sticky='EW')
self.entryBefore.insert(0, 'YYYY/MM/DD')
self.buttonBefore = Button(gui,text=u"Before",command=self.getBeforeTxT)
self.buttonBefore.grid(column=2,row=1)
#PLACE
self.textStation = Label(gui, text="Select your place : ")
self.textStation.grid(column=0,row=2)
self.optionList = ('Paris', 'Tokyo', 'Londres','NewYork')
self.var = StringVar(gui)
self.optionmenu = apply(OptionMenu, (gui,self.var) + tuple(self.optionList))
self.optionmenu.grid(column=1, row=2,sticky="WE")
self.buttonStation = Button(gui,text=u"Place", command = self.getPlaceValue)
self.buttonStation.grid(column=2,row=2)
#QUIT
self.bouttonQuit = Button(gui, text='Quit', command = gui.quit)
self.bouttonQuit.grid(column=2,row=3)
#SAVE AS
self.buttonSaveAs = Button(gui,text=u"Save As", command = self.printData)
self.buttonSaveAs.grid(column=1,row=3)
#get the text from the emtry entryAfter
#I don't know how i can use afterTxTd,beforeTxTd,stationPlace in MyProgram
#to put in my function queryD
def getAfterTxT(self):
afterTxT = self.entryAfter.get()
afterTxTd=datetime.strptime(afterTxT,'%Y/%m/%d')
def getBeforeTxT(self):
beforeTxT = self.entryBefore.get()
beforeTxTd= datetime.strptime(beforeTxT,'%Y/%m/%d')
def getPlaceValue(self):
stationPlace = self.var.get()
#This is the error
def printData(self):
MyProgram.writeIndex()
gui = Tk()
app = App(gui)
gui.mainloop()
gui.destroy()
When the user click on the button after(after putting a date), i wanted to run MyProgram, run the script, write the information in a text file and show the results with the function readIndex(). After that if the user wanted to save the file, he has just to push the button Save As. Have you any suggestion to help me? Any tutorial? Do I have to create one class for each function in MyProgram and Do i have to create an 'init'for each class? Thanx to read this post ! Have a nice day !
Upvotes: 0
Views: 1589
Reputation: 142859
If you have first script in file myfirst_script.py
then you import this as
import myfirst_script
and then you can use it as
my_program = myfirst_script.MyProgram()
my_program.writeIndex()
If you have first script in file MyProgram.py
then you import this as
import MyProgram
and then you can use it as
my_program = MyProgram.MyProgram()
my_program.writeIndex()
or you can import as
from MyProgram import MyProgram
and then you can use it as
my_program = MyProgram()
my_program.writeIndex()
BTW: all functions in class need self
as first argument because calling
my_program.writeIndex()
is (internally for python) almost like calling
writeIndex(my_program)
BTW: classes make order in code but I think you don't need class MyProgram
and you could use only separated function in first script.
Then you could use it (without all self
s) as
import myfirst_script
myfirst_script.writeIndex()
Eventually you would need global
in some functions which change values in d
and d_string
.
Upvotes: 1