所以,我对如何在Tkinter做一些事情有了更多的好奇心。这不是必需的,但我有点想实现它。无论如何,我想要做的是当选择apSelect列表框中的一个项目时,我想创建一个或两个多列表框,然后从文件中的数据填充(为了简单起见,让我们说它来自预设列表。)除此之外,如果在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()
答案 0 :(得分:1)
使用网格几何管理器,您可以轻松地在给定事件上显示/消失小部件,就像我在下面的示例中所做的那样。
pack
有一个类似的pack_forget
方法,但我想如果你的小部件在同一个地方消失/重新出现可能会有点困难,如果它不局限于静止的,专用框架。
此代码中的窗口小部件在按下按钮时显示/消失,但如果您了解如何在Tkinter中绑定事件,则此方法可用于使窗口小部件(dis)出现在您喜欢的任何事件上(请参阅更新信息的bind
方法。)
import Tkinter as tk
class App(tk.Frame):
def __init__(self,master):
tk.Frame.__init__(self,master)
self.b=tk.Button(self,text='hide',command=self.hide)
self.lbl=tk.Label(self,text="This is a label you can hide")
self.b.grid(row=0,column=0)
self.lbl.grid(row=0,column=1)
def hide(self):
self.lbl.grid_forget()
self.b.config(command=self.show,text='show')
def show(self):
self.lbl.grid(row=0,column=1)
self.b.config(command=self.hide,text='hide')
if __name__ == '__main__':
root = tk.Tk()
f=App(root)
f.grid(row=0,column=0)
root.mainloop()
我不完全确定我已经在这里回答了整个问题 - 你的问题中有很多要解决...如果你有其他问题,请告诉我......