コード例 #1
0
ファイル: GUI.py プロジェクト: bradtaniguchi/IMSRoomRenter
class Application(tk.Tk):
    # inspired from http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
    """
    Primary Application manager, holds all frames, and creates and handles static menubar
    """
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)  # container to be used for all frames
        container.pack(side="top", fill="both", expand=False)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.title("IMSRoomRenter v.1.1")
        iconpath = os.path.join(os.path.dirname(__file__), "bin/IMSRoomRenter.ico")
        self.iconbitmap(iconpath)  # bugs out?
        self.mydebugstring = " "
        self.minsize(width=300, height=175)  # Determined constant window size?
        self.maxsize(width=600, height=175)
        self.resizable(width=False, height=False)
        self.menubar = tk.Menu()  # is this OK????
        self.create_menubar()  # creates static menubar
        self.updateflag = True
        self.menubar.add_separator()
        self.menubar.add_command(label="Back", command=lambda: self.show_frame("PrimaryPage"))
        self.frames = {}  # array of frames
        self.databaseposition = 'bin/Sqlite/StudentDatabase.sqlite'  # default database position
        self.loggedinstudents = []  # to see who is logged in. Gathered from Clockout
        self.roomsavail = 5
        self.mydatabasechecker = DataBaseInterface()
        self.mydatabasechecker.checkifdatabaseexists(self.databaseposition)
        for F in (PrimaryPage, ClockIn, ClockOut):  # initialize all frame/Classes
            page_name = F.__name__
            frame = F(container, self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="NSEW")
        self.show_frame("PrimaryPage")

    def primupdatescreens(self):
        """
        Calls updateframes in all frames. Also changes the updateflag, to update the database calls
        :return:
        """
        if self.updateflag:
            self.sysprint(">DEBUG: PrimUpdating screens...")
            self.frames["ClockOut"].updatescreens()  # CHANGE ALL OF THESE TO ACCEPT THE ARRAY! easier to read!
            self.loggedinstudents = self.frames["ClockOut"].mystudentcollection.listofstudents
            self.frames["ClockIn"].updatescreens()  # update WHICH rooms are available
            self.frames["PrimaryPage"].updatescreens()  # update how many rooms available!
            self.updateflag = False

    def show_frame(self, page_name):
        """
        Show a Frame for a given page name
        :param page_name: page to change to
        """
        self.primupdatescreens()  # EDIT THIS
        self.sysprint(">DEBUG: Showing frame: " + str(page_name))
        f = self.frames[page_name]
        f.tkraise()

    def create_menubar(self):
        """
        creates menubar object, which all menu buttons are attatched to.
        Calls: _create_file_menu, _create_edit_menu, _create_help_menu
        to create their respective menus.
        """
        self.configure(menu=self.menubar)
        self._create_file_menu()
        self._create_edit_menu()
        self._create_view_menu()
        self._create_help_menu()

    def _create_file_menu(self):
        """creates filemenu, and cascade. """
        filemenu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="StartIMSAdmin", command=lambda: self.startimsadmin())  # DUMB referenced!
        filemenu.add_command(label="Quit", command=self.quitprogram)  # DUMB referenced!

    def _create_edit_menu(self):
        """creates editmenu, and cascade. """
        editmenu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="Edit", menu=editmenu)
        editmenu.add_command(label="Go to MainPage", command=lambda: self.show_frame("PrimaryPage"))
        editmenu.add_command(label="ShowDebugBox", command=lambda: self.showdebugbox())
        # editmenu.add_command(label="TestDaily", command=lambda: self.testdaily())

    def _create_view_menu(self):
        viewmenu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="View", menu=viewmenu)
        viewmenu.add_command(label="ViewRoom1", command=lambda: self.showroomview(1))  # CHANGE!
        viewmenu.add_command(label="ViewRoom2", command=lambda: self.showroomview(2))  # CHANGE!
        viewmenu.add_command(label="ViewRoom3", command=lambda: self.showroomview(3))  # CHANGE!
        viewmenu.add_command(label="ViewRoom4", command=lambda: self.showroomview(4))  # CHANGE!
        viewmenu.add_command(label="ViewRoom5", command=lambda: self.showroomview(5))  # CHANGE!

    def _create_help_menu(self):
        """creates helpmenu, and cascade. """
        helpmenu = tk.Menu(self.menubar, tearoff=False)
        self.menubar.add_cascade(label="Help", menu=helpmenu)
        helpmenu.add_command(label="About", command=lambda: self.showaboutmenu())  # TEST!

    def showroomview(self, roomnumber):
        """
        Displays a tk.TopLevel window of the room
        :param roomnumber: Number of Rooms to view
        """
        self.sysprint("Popup RoomView: " + str(roomnumber))
        if roomnumber == 1:
            mypopup = RoomView("Room1", "bin/Room1.png")
            mypopup.mainloop()
        elif roomnumber == 2:
            mypopup = RoomView("Room2", "bin/Room2.png")
            mypopup.mainloop()
        elif roomnumber == 3:
            mypopup = RoomView("Room3", "bin/Room3.png")
            mypopup.mainloop()
        elif roomnumber == 4:
            mypopup = RoomView("Room4", "bin/Room4.png")
            mypopup.mainloop()
        elif roomnumber == 5:
            mypopup = RoomView("Room5", "bin/Room5.png")
            mypopup.mainloop()
        elif roomnumber == 0:
            mypopup = Popups("ERROR!", "No Room Choosen!")
            mypopup.mainloop()
        else:
            mypopup = Popups("ERROR!", "Internal Error, Bad RoomView Request!")
            mypopup.mainloop()

    @staticmethod
    def showaboutmenu():
        """
        Displays the About Menu
        """
        myaboutmenu = AboutMenu()
        myaboutmenu.mainloop()

    def startimsadmin(self):
        """
        Starts IMSAdmin in Console Window, program still runs during this time
        So try not to interact with the Console and still use the program. Exit the
        IMSAdmin in the console.
        """
        self.sysprint("IMSAdmin Starting...")
        myadmin = IMSAdmin()
        myadmin.prompt()

    def quitprogram(self):
        """
        Quits the Entire Program Application
        """
        self.sysprint(">:Quiting program via filemenu")
        self.quit()
        self.destroy()
        exit(0)  # exit program

    def sysprint(self, appendtext):
        """
        Updates the string of debugbox, adds newline and timestamp
        :param appendtext: text to append to debugbox
        """
        thetime = (str(datetime.now().time().hour) + ":" + str(datetime.now().time().minute) + ":" +
                   str(datetime.now().time().second))
        self.mydebugstring += (">["+str(thetime)+"]" + appendtext + "\n")

    def showdebugbox(self):
        """
        Displays Current Contents of Database, using DatabaseInterface
        """
        mydebugbox = DebugBox(self, "DebugBox")  #
        mydebugbox.mainloop()  # needs work