我有很多關於python class和tkinter的問題。 我以前從未使用過,但我試圖製作我的第一個程序並將其鏈接到gui。 我分別做了我的程序和我的GUI,這是一個不好的選擇?如何使用class來鏈接tkinter gui和程序?
我的代碼,第一部分是:
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()]
我的GUI是:
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()
當後的按鈕,用戶點擊,我想跑MyProgram(把一個日期之後),運行腳本,將信息寫入文本文件並使用函數readIndex()顯示結果。 之後,如果用戶想保存該文件,他只需按下另存爲按鈕即可。 你有什麼建議可以幫助我嗎?任何教程?我是否必須爲MyProgram中的每個函數創建一個類,並且是否必須爲每個類創建'init'? Thanx閱讀此文章!祝你今天愉快 !
Thx for your help – Jguillot