mardi 14 mars 2017

Python Tkinter Widgets - Scrollbars not working and multi-select check boxes

I'm having two problems with my Python Tkinter code below, which is a series of buttons and check boxes in the right window frame:

  1. The scrollbars work for the drawn on boxes and text only, but the buttons and check boxes don't move.

  2. I'd like to make it possible to use certain check boxes to select lower level check boxes. Ex 1: Check box 1 on the top selects all check boxes in the 4 rows below it. Ex 2: All check boxes directly above the 8 buttons on each row would select all 8 of the check boxes below it, such as checkbox s14 selects all check boxes on the bottom row (checkbox 141 thru 148). Ex 3: Select one to many check boxes as needed (technically this works for singular selections, but a previous version of my code made it so that selecting one check box selected them all, which is undesired - so I removed this code).

I was hoping to get some direction on how I could accomplish these tasks. Any advice would be appreciated.

Code:

from Tkinter import *

class GUI(Frame, Toplevel):

    def __init__(self, master):

        Frame.__init__(self, master)
        self.master = master
        self.initUI()

    def initUI(self):

        self.master.title('Test GUI')
        self.pack(fill=BOTH, expand=1, side=RIGHT)

        self.left_fra = Frame(self.master, bg='grey', bd=2)
        self.left_fra.pack(side=LEFT, anchor=NW, ipadx=5, ipady=400)
        Label(self.left_fra, text="Test GUI", bg='light blue', justify=CENTER, font=('Tahoma', 20)).pack(padx=5,pady=5)

        self.frame = Frame(self.master)
        self.right_fra = Canvas(self.frame, bg='light blue', bd=2, scrollregion=(0,0,1400,700))

        self.right_fra.pack(side=RIGHT, anchor=E, ipadx=800, ipady=400, fill=BOTH)
        self.frame.pack(expand=False, padx=0,pady=0)

        self.right_fra.create_rectangle(1100, 135, 1326, 578, outline="black")
        self.right_fra.create_rectangle(1100, 165, 1326, 273, outline="black")
        self.right_fra.create_rectangle(1100, 165, 1326, 373, outline="black")
        self.right_fra.create_rectangle(1100, 165, 1326, 473, outline="black")

        self.r1_Checkbox = Checkbutton(self.right_fra, bg='light blue', text="1", font=(20))
        self.r1_Checkbox.place(x=1115, y=136)

        self.s11_Checkbox = Checkbutton(self.right_fra, bg='light blue', text="1-1", font=(20))
        self.s11_Checkbox.place(x=1115, y=175)

        self.s12_Checkbox = Checkbutton(self.right_fra, bg='light blue', text="1-2", font=(20))
        self.s12_Checkbox.place(x=1115, y=275)

        self.s13_Checkbox = Checkbutton(self.right_fra, bg='light blue', text="1-3", font=(20))
        self.s13_Checkbox.place(x=1115, y=375)

        self.s114_Checkbox = Checkbutton(self.right_fra, bg='light blue', text="1-4", font=(20))
        self.s114_Checkbox.place(x=1115, y=475)

        self.f111_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n1")
        self.f111_Button.place(x=1115, y=200)

        self.f112_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n2")
        self.f112_Button.place(x=1141, y=200)

        self.f113_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n3")
        self.f113_Button.place(x=1167, y=200)

        self.f114_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n4")
        self.f114_Button.place(x=1193, y=200)

        self.f115_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n5")
        self.f115_Button.place(x=1219, y=200)

        self.f116_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n6")
        self.f116_Button.place(x=1245, y=200)

        self.f117_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n7")
        self.f117_Button.place(x=1271, y=200)

        self.f118_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n1\n8")
        self.f118_Button.place(x=1297, y=200)

        self.f111_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f111_Checkbox.place(x=1115, y=248)

        self.f112_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f112_Checkbox.place(x=1141, y=248)

        self.f113_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f113_Checkbox.place(x=1167, y=248)

        self.f114_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f114_Checkbox.place(x=1193, y=248)

        self.f115_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f115_Checkbox.place(x=1219, y=248)

        self.f116_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f116_Checkbox.place(x=1245, y=248)

        self.f117_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f117_Checkbox.place(x=1271, y=248)

        self.f118_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f118_Checkbox.place(x=1297, y=248)

        self.f121_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n1")
        self.f121_Button.place(x=1115, y=300)

        self.f122_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n2")
        self.f122_Button.place(x=1141, y=300)

        self.f123_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n3")
        self.f123_Button.place(x=1167, y=300)

        self.f124_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n4")
        self.f124_Button.place(x=1193, y=300)

        self.f125_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n5")
        self.f125_Button.place(x=1219, y=300)

        self.f126_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n6")
        self.f126_Button.place(x=1245, y=300)

        self.f127_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n7")
        self.f127_Button.place(x=1271, y=300)

        self.f128_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n2\n8")
        self.f128_Button.place(x=1297, y=300)

        self.f121_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f121_Checkbox.place(x=1115, y=348)

        self.f122_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f122_Checkbox.place(x=1141, y=348)

        self.f123_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f123_Checkbox.place(x=1167, y=348)

        self.f124_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f124_Checkbox.place(x=1193, y=348)

        self.f125_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f125_Checkbox.place(x=1219, y=348)

        self.f126_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f126_Checkbox.place(x=1245, y=348)

        self.f127_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f127_Checkbox.place(x=1271, y=348)

        self.f128_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f128_Checkbox.place(x=1297, y=348)

        self.f131_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n1")
        self.f131_Button.place(x=1115, y=400)

        self.f132_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n2")
        self.f132_Button.place(x=1141, y=400)

        self.f133_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n3")
        self.f133_Button.place(x=1167, y=400)

        self.f134_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n4")
        self.f134_Button.place(x=1193, y=400)

        self.f135_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n5")
        self.f135_Button.place(x=1219, y=400)

        self.f136_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n6")
        self.f136_Button.place(x=1245, y=400)

        self.f137_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n7")
        self.f137_Button.place(x=1271, y=400)

        self.f138_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n3\n8")
        self.f138_Button.place(x=1297, y=400)

        self.f131_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f131_Checkbox.place(x=1115, y=448)

        self.f132_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f132_Checkbox.place(x=1141, y=448)

        self.f133_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f133_Checkbox.place(x=1167, y=448)

        self.f134_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f134_Checkbox.place(x=1193, y=448)

        self.f135_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f135_Checkbox.place(x=1219, y=448)

        self.f136_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f136_Checkbox.place(x=1245, y=448)

        self.f137_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f137_Checkbox.place(x=1271, y=448)

        self.f138_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f138_Checkbox.place(x=1297, y=448)

        self.f141_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n1")
        self.f141_Button.place(x=1115, y=500)

        self.f142_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n2")
        self.f142_Button.place(x=1141, y=500)

        self.f143_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n3")
        self.f143_Button.place(x=1167, y=500)

        self.f144_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n4")
        self.f144_Button.place(x=1193, y=500)

        self.f145_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n5")
        self.f145_Button.place(x=1219, y=500)

        self.f146_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n6")
        self.f146_Button.place(x=1245, y=500)

        self.f147_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n7")
        self.f147_Button.place(x=1271, y=500)

        self.f148_Button = Button(self.right_fra, width=4, font=('tahoma', 6), text="BU\n1\n4\n8")
        self.f148_Button.place(x=1297, y=500)

        self.f141_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f141_Checkbox.place(x=1115, y=548)

        self.f142_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f142_Checkbox.place(x=1141, y=548)

        self.f143_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f143_Checkbox.place(x=1167, y=548)

        self.f144_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f144_Checkbox.place(x=1193, y=548)

        self.f145_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f145_Checkbox.place(x=1219, y=548)

        self.f146_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f146_Checkbox.place(x=1245, y=548)

        self.f147_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f147_Checkbox.place(x=1271, y=548)

        self.f148_Checkbox = Checkbutton(self.right_fra, bg='light blue')
        self.f148_Checkbox.place(x=1297, y=548)

        self.hbar = Scrollbar(self.right_fra, orient=HORIZONTAL)
        self.hbar.config(command=self.right_fra.xview)
        self.hbar.pack(fill=BOTH, side=BOTTOM)
        self.vbar = Scrollbar(self.right_fra, orient=VERTICAL)
        self.vbar.config(command=self.right_fra.yview)
        self.vbar.pack(fill=BOTH, side=RIGHT)
        self.right_fra.config(xscrollcommand=self.hbar.set, yscrollcommand=self.vbar.set)

class Controller:

    def __init__(self, root):

        self.root = root
        self.view = GUI(self.root)

if __name__ == '__main__':
    root = Tk()
    myapp = GUI(root)
    root.mainloop()




Aucun commentaire:

Enregistrer un commentaire