0

所以,我对如何在 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()
4

1 回答 1

1

使用网格几何管理器,您可以轻松地让小部件在给定事件上出现/消失,就像我在下面的示例中所做的那样。

pack有一个类似的pack_forget方法,但我想如果它不局限于固定的专用框架,让你的小部件消失/重新出现在同一个地方可能会有点困难。

此代码中的小部件会在按下按钮时出现/消失,但如果您了解如何在 Tkinter 中绑定事件(查找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()

我不完全确定我已经在这里回答了整个问题-您的问题有很多需要整理...如果您还有其他问题,请告诉我...

于 2012-06-08T18:55:55.410 回答