Bonjour, Je viens de creer mon premier "vrai" programme en python qui permet de rechercher des valeurs dans un dictionnaire, de les ecrire dans un fichier text puis de les lire a partrir de ce fichier
Je voudrais le lier a une GUI afin d'avoir un programme 'complet'. J'ai donc créé ma GUI á cote de ce programme sans penser aux liens aui doivent lier mes deux bouts de code.
Ce que je voudrais faire avec la GUI :
1- Choisir une date et suivant le bouton (After ou Before) le stocker dans la variable timeBefore ou timeAfter.
2- Selectionner une ville et la stocker variable stationPlace.
3-Un bouton 'run', qui injecte les données récuperées dans le programme (dans la partie query) et lance ce dernier.
4-Sauvegarder avec le bouton 'save as' les resultats de ma recherche grace a ma function writeIndex.
Avez vous un point de depart pour m'aider a faire le lien entre les 2 parties?
Voici mon code:
MyProgram:
MyGUI :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 from datetime import datetime #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 d.items() if values[2]>timeAfter and values[2]<timeBefore] else: answer = [[key,values] for key,values in d.items() if values[2]>timeAfter and values[2]<timeBefore and values[1]==place] return answer #function to write the results of queryD def writeIndex(): #open index.txt and give the right to write myFile=open('output.txt', 'w') for l in 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()]
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 from Tkinter import * from datetime import * class App: def __init__(self, gui): gui = Frame(gui) gui.grid() #AFTER############################################################ #Create Text self.text = Label(gui, text="Put a date : ") #give to Text a place self.text.grid(column=0,row=0) #Create an area to write text self.entry = Entry(gui) self.entry.grid(column=1,row=0,sticky='EW') self.entry.insert(0, 'YYYY/MM/DD') self.entry.focus_set() #Create a button self.buttonAfter = Button(gui,text=u'After', command=self.getAfter) self.buttonAfter.grid(column=2,row=0) self.buttonBefore = Button(gui,text=u"Before",command=self.getBefore) self.buttonBefore.grid(column=3,row=0) #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.buttonQuit = Button(gui, text='Quit', command = gui.quit) self.buttonQuit.grid(column=3,row=3) #SAVE AS########################################################### self.buttonSaveAs = Button(gui,text=u"Save As") self.buttonSaveAs.grid(column=2,row=3) #RUN########################################################### self.buttonRun=Button(gui,text=u"Run") self.buttonRun.grid(column=1,row=3) #function to get timeAfter as a datetime and inject into MyProgramm def getAfter(self): afterString = self.entry.get() timeAfter=datetime.strptime(afterString,'%Y/%m/%d') print 'after ' + afterString return timeAfter #function to get timeBefore as a datetime and inject into MyProgramm def getBefore(self): beforeString = self.entry.get() timeBefore= datetime.strptime(beforeString,'%Y/%m/%d') print 'before ' + beforeTxT return timeBefore #function to get sationPlace as a string and inject into MyProgramm def getPlaceValue(self): stationPlace = self.var.get() return stationPlace gui = Tk() app = App(gui) gui.mainloop() gui.destroy()
Partager