所以,我对如何在 Tkinter 中做某事更加好奇。这不是必需的,但我有点想实现它。无论如何,我想要做的是,选择Apselect ListBox中的一个项目时,我想创建一个或两个多个listbox,然后从文件中填充来自数据的数据(用于简单缘故,让我们只是从预设中说出它列表。)除此之外,如果在 apSelect 中选择了不同的条目,我希望它们消失。另一件事是抓取在这些动态列表框中选择的任何数据(假设它们存在)。这是使用 Tkinter 的代码。真正唯一重要的部分是init_方法(这可能是我可以为动态生成设置的地方)和 generate 方法(我需要提取数据的地方)。
class App:
def __init__(self, master):
dateFrame= Frame(master)
dateFrame.pack()
apSelectFrame = Frame(master)
apSelectFrame.pack()
entryFrame = Frame(master)
entryFrame.pack()
buttonFrame = Frame(master)
buttonFrame.pack()
Label(dateFrame, text="Period:").grid(row=0)
self.period = Entry(dateFrame)
self.period.grid(row=0, column=1)
Label(dateFrame, text="Year:").grid(row=1)
self.year = Entry(dateFrame)
self.year.grid(row=1, column=1)
Label(apSelectFrame, text="Select Report Type:").grid(row=0)
self.apSelect = Listbox(apSelectFrame, selectmode=SINGLE, height=3)
self.apSelect.grid(row=1)
for item in ["Artists", "Publishers", "Both"]:
self.apSelect.insert(END, item)
self.allTheThings = IntVar()
self.checkAll = Checkbutton(apSelectFrame, text="All?", variable=self.allTheThings)
self.checkAll.grid(row=1, column=1)
Label(entryFrame, text="Client Name: ").grid(row=0)
self.client = Entry(entryFrame)
self.client.grid(row=0, column=1)
Label(entryFrame, text="Publisher Name: ").grid(row=1)
self.pub = Entry(entryFrame)
self.pub.grid(row=1, column=1)
self.generateBt = Button(buttonFrame, text="Generate!", command=self.generate)
self.generateBt.pack(side=LEFT)
self.resetBt = Button(buttonFrame, text="Reset!", command=self.reset)
self.resetBt.pack(side=LEFT)
self.quitBt = Button(buttonFrame, text="Quit!", fg="red", command=buttonFrame.quit)
self.quitBt.pack(side=LEFT)
def fileChecker(self, filename):
if(not os.path.isfile(filename)):
return "Missing file: "+filename+"\n"
def checkForFiles(self, srFilename):
errorFlag = False
errorReport = []
errorReport.append(self.fileChecker("albums.csv"))
errorReport.append(self.fileChecker("clients.csv"))
errorReport.append(self.fileChecker("composition_copyright_holders.csv"))
errorReport.append(self.fileChecker("compositions.csv"))
errorReport.append(self.fileChecker("copyright_holders.csv"))
errorReport.append(self.fileChecker("track_compositions.csv"))
#errorReport.append(self.fileChecker("track_copyright_holders.csv"))
errorReport.append(self.fileChecker("tracks.csv"))
errorReport.append(self.fileChecker("copyright_holders_previously_owed.csv"))
errorReport.append(self.fileChecker("artist_override.csv"))
errorReport.append(self.fileChecker(srFilename))
if(errorFlag):
tkMessageBox.showerror("Error",
"Either select all or enter publisher name!")
else:
return
def generate(self):
#TODO File checker
month = self.period.get()
#month = 'q1'
year = self.year.get()
#year = '2012'
clname = self.client.get()
pubname = self.pub.get()
report_type = self.apSelect.curselection()
#report_type = '1'
allTheThings = self.allTheThings.get()
#allTheThings = True
if len(month)==0:
tkMessageBox.showerror("Error",
"You forgot to enter a period!")
return
if len(year)==0:
tkMessageBox.showerror("Error",
"You forgot to enter a year!")
return
if len(report_type)==0:
tkMessageBox.showerror("Error",
"You forgot to select a report type!")
return
if report_type[0]=='0' and allTheThings==0 and clname=="":
tkMessageBox.showerror("Error",
"Either select all or enter a client name!")
return
if report_type[0]=='1' and allTheThings==0 and pubname=="":
tkMessageBox.showerror("Error",
"Either select all or enter publisher name!")
return
if report_type[0]=='2' and allTheThings==0 and clname=="" and pubname=="":
tkMessageBox.showerror("Error",
"Either select all or enter a client and publisher name!")
return
if(not os.path.exists("reports")):
os.makedirs("reports")
if(not os.path.exists('reports/'+month+'_'+year+'')):
os.makedirs('reports/'+month+'_'+year)
if(not os.path.exists('reports/'+month+'_'+year+'/artists')):
os.makedirs('reports/'+month+'_'+year+'/artists')
if(not os.path.exists('reports/'+month+'_'+year+'/publishers')):
os.makedirs('reports/'+month+'_'+year+'/publishers')
if(not os.path.exists('reports/'+month+'_'+year+'/artists/empty')):
os.makedirs('reports/'+month+'_'+year+'/artists/empty')
if(not os.path.exists('reports/'+month+'_'+year+'/publishers/empty')):
os.makedirs('reports/'+month+'_'+year+'/publishers/empty')
srFilename="sr1_"+month+"_"+year+".csv"
self.checkForFiles(srFilename)
tester=ACR_CALC()
tester.calc(month, year, clname, pubname, report_type, allTheThings,srFilename)
def reset(self):
print "RESETING!"
self.period.delete(0, END)
self.year.delete(0, END)
self.allTheThings.set(0)
self.client.delete(0, END)
self.pub.delete(0, END)
self.apSelect.select_clear(0,END)
root = Tk()
app = App(root)
root.mainloop()