lundi 26 février 2018

Scrollable Checkbox Canvas Within Main Python Frame

I'm working on creating a GUI for a small application, and it being my first endeavor into Python GUI I'm a bit overwhelmed. My goal here, is to create a GUI that has some buttons and entry fields and a progress bar in it, and also in that same frame, have a canvas (or frame? not sure) that has a vertical scrollbar in it so that I can populate that canvas with checkboxes and be able to scroll the list within the main window.

I've got pretty much everything laid out, but without the scrollable canvas inside the frame, I'm at a bit of a standstill. Is there a way I can implement this into what I've already got here?

class MainWindow(Frame):

def __init__(self):
    Frame.__init__(self)
    self.master.title("WIP")
    # self.master.minsize(0, 0)
    self.grid(sticky=E+W+N+S)
    self.columnconfigure(1, weight=1)

    self.var_det = IntVar(self)
    self.inputtext = StringVar()
    self.outputtext = StringVar()
    self.lastknowninput = StringVar()
    self.lastknownoutput = StringVar()
    self.defaultDir = "\\"
    self.currentInputDir = ""
    self.currentOutputDir = ""

    scroll = Scrollbar(self)

    self.pbar_det = ttk.Progressbar(self, orient="horizontal", length=750, mode="determinate", variable=self.var_det, maximum=100)
    self.pbar_det.grid(row=15, column=0, pady=2, padx=2, sticky=E+W+N+S, columnspan=5)

    Button(self, text="Browse", command=self.open_inputdir).grid(row=0, column=3, pady=2, padx=2, sticky=E+W+N+S)
    Button(self, text="Browse", command=self.open_outputdir).grid(row=1, column=3, pady=2, padx=2, sticky=E+W+N+S)



    canvas = Canvas(self, yscrollcommand=scroll.set).grid(row=2, column=0, padx=2, pady=2, sticky=E+W+N+S, columnspan=3, rowspan=10)



    # Need to add function to set all checkboxes state to checked
    Button(self, text="Select All").grid(row=4, column=4, pady=2, padx=2, sticky=E+W+N+S)
    Button(self, text="Select None").grid(row=6, column=4, pady=2, padx=2, sticky=E+W+N+S)

    # Need to add function to call main python file, also to increment values to control progressbar as well as progress text
    Button(self, text="Run", command=self.doStuff).grid(row=8, column=4, pady=2, padx=2, sticky=E+W+N+S, rowspan=2)
    Button(self, text="Cancel", command=self.stopStuff).grid(row=10, column=4, pady=2, padx=2, sticky=E+W+N+S, rowspan=2)

    # Can perform inputfile.get() to pull off value inside of inputfile entry field
    input = Entry(self, textvariable=self.inputtext).grid(row=0, column=1, pady=2, padx=2, sticky=E+W, columnspan=2)
    output = Entry(self, textvariable=self.outputtext).grid(row=1, column=1, pady=2, padx=2, sticky=E+W, columnspan=2)

    Label(self, text="Module Progress Text Here").grid(row=13, column=0, pady=2, padx=2, sticky=W, columnspan=5)
    Label(self, text="Input File Path").grid(row=0, column=0, pady=2, padx=2, sticky=W)
    Label(self, text="Output File Path").grid(row=1, column=0, pady=2, padx=2, sticky=W)

def open_inputdir(MainWindow):
    if not MainWindow.inputtext.get():
        MainWindow.inputfilename = filedialog.askdirectory(initialdir="\\", title="Select Folder")
        if MainWindow.inputfilename:
            MainWindow.inputtext.set(MainWindow.inputfilename)
            MainWindow.lastknowninput.set(MainWindow.inputfilename)
    else:
        MainWindow.inputfilename = filedialog.askdirectory(initialdir=MainWindow.currentInputDir, title="Select Folder")
        if MainWindow.inputfilename:
            MainWindow.inputtext.set(MainWindow.inputfilename)
            MainWindow.lastknowninput.set(MainWindow.inputfilename)

def open_outputdir(MainWindow):
    if not MainWindow.outputtext.get():
        MainWindow.outputfilename = filedialog.askdirectory(initialdir="\\", title="Select Folder")
        if MainWindow.outputfilename:
            MainWindow.outputtext.set(MainWindow.outputfilename)
            MainWindow.lastknownoutput.set(MainWindow.outputfilename)
    else:
        MainWindow.outputfilename = filedialog.askdirectory(initialdir=MainWindow.currentOutputDir, title="Select Folder")
        if MainWindow.outputfilename:
            MainWindow.outputtext.set(MainWindow.outputfilename)
            MainWindow.lastknownoutput.set(MainWindow.outputfilename)

def doStuff(MainWindow):
    MainWindow.pbar_det.start()

def stopStuff(MainWindow):
    MainWindow.pbar_det.stop()
    MainWindow.var_det.set(0)

def set(self, val):
    self.var_det.set(val)


if __name__ == "__main__":
            MainWindow().mainloop()

Here's a snip from what I've got generating right now, the large open space is where the canvas has created, but I'm haven't been able to get scroll to appear in it so far.

Current State

Help is greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire