def ServiceState(count, column, columnspan, ServiceName):
    FLAG = ServiceOpt.getServiceState(ServiceName)
    if FLAG == 1:
        printPNG(GREEN, count, column, columnspan)
        return True
    elif FLAG == 0:
        printPNG(RED, count, column, columnspan)
        return True
    elif FLAG == -1:
        printPNG(YELLOW, count, column, columnspan)
        return False
def printMenuButton(tk, text, mes, count, column):
    if ServiceOpt.getServiceState(mes) == -1:
        btn = Button(tk,
                     text=text,
                     anchor='w',
                     height=1,
                     relief=FLAT,
                     activeforeground="blue")
        btn.bind("<Button-1>",
                 lambda index=None, title="错误", mes="服务未安装": ServiceOpt.
                 createWindow(title, mes))
        btn.bind("<Enter>", lambda event: event.widget.config(fg="blue"))
        btn.bind("<Leave>", lambda event: event.widget.config(fg="black"))
        btn.grid(row=count, column=column, sticky=W)
    else:
        btn = Menubutton(tk,
                         text=text,
                         anchor='w',
                         height=1,
                         relief=FLAT,
                         activeforeground="blue")
        btn.grid(row=count, column=column, sticky=W)
        fileMenu = Menu(btn, tearoff=False)
        fileMenu.add_radiobutton(label=START,
                                 value=1,
                                 command=lambda: ServiceOpt.ServiceStart(mes))
        fileMenu.add_radiobutton(label=STOP,
                                 value=2,
                                 command=lambda: ServiceOpt.ServiceStop(mes))
        fileMenu.add_radiobutton(
            label=RE_START,
            value=3,
            command=lambda: ServiceOpt.ServiceReStart(mes))
        fileMenu.add_radiobutton(
            label=LOG_FILE,
            value=4,
            command=lambda: ServiceOpt.openNoteByName(LogListAddress, mes))
        fileMenu.add_radiobutton(
            label=SET_START_AUTO,
            value=5,
            command=lambda: ServiceOpt.ServiceSetStartAuto(mes, "auto"))
        fileMenu.add_radiobutton(
            label=SET_START_DEMAND,
            value=6,
            command=lambda: ServiceOpt.ServiceSetStartAuto(mes, "demand"))
        fileMenu.add_radiobutton(
            label=SET_START_DISABLED,
            value=7,
            command=lambda: ServiceOpt.ServiceSetStartAuto(mes, "disabled"))
        btn.config(menu=fileMenu)
def StateReFresh(tk, ServiceNameList, GREEN, RED, YELLOW):
    for count in range(int(len(ServiceNameList) / 3)):
        FLAG = ServiceOpt.getServiceState(ServiceNameList[int(count * 3)])

        if FLAG == 1:
            printComp.printPNG(GREEN, count, 3, 1)
            printComp.printLabel(tk, STATE_RUNNING, count, 4, 1)
        elif FLAG == 0:
            printComp.printPNG(RED, count, 3, 1)
            printComp.printLabel(tk, STATE_STOPPED, count, 4, 1)
        elif FLAG == -1:
            printComp.printPNG(YELLOW, count, 3, 1)
            printComp.printLabel(tk, STATE_UNINSTALLED, count, 4, 1)
def ServiceState(tk, count, column, columnspan, ServiceName):
    FLAG = ServiceOpt.getServiceState(ServiceName)
    if FLAG == 1:
        printPNG(GREEN, count, column, columnspan)
        printLabel(tk, "已启动", count, column + 1, columnspan)
        return True
    elif FLAG == 0:
        printPNG(RED, count, column, columnspan)
        printLabel(tk, "未启动", count, column + 1, columnspan)
        return True
    elif FLAG == -1:
        printPNG(YELLOW, count, column, columnspan)
        printLabel(tk, "未安装", count, column + 1, columnspan)
        return False
 def btnLogFile(LogAddress, LogFile):
     ServiceOpt.openNoteByName(LogAddress, LogFile)
     fileMenu.entryconfig(LOG_FILE, state=DISABLED)
 def btnReStart(mes):
     ServiceOpt.ServiceReStart(mes)
     fileMenu.entryconfig(RE_START, state=DISABLED)
     printLabel(tk, STATE_RE_STARTING, count, 4, 1)
 def btnStop(mes):
     ServiceOpt.ServiceStop(mes)
     fileMenu.entryconfig(STOP, state=DISABLED)
     printLabel(tk, STATE_STOPPING, count, 4, 1)
def printMenuButton(tk, text, mes, count, column, LogAddress, LogFile,
                    InstallAddress):
    FLAG = ServiceOpt.getServiceState(mes)

    # 判断地址
    SetupAddress = InstallAddress
    if mes == 'Face_Nginx':
        SetupAddress = LoadConfig.loadConfig("file_address", "NginxWebAddress")
    elif mes == 'Face_Mosquitto':
        SetupAddress = LoadConfig.loadConfig("file_address",
                                             "MosquittoAddress")

    btn = Menubutton(tk,
                     text=text,
                     anchor='w',
                     height=1,
                     relief=FLAT,
                     activeforeground="blue")
    btn.grid(row=count, column=column, sticky=W)

    fileMenu = Menu(btn, tearoff=False)

    # 启动服务
    fileMenu.add_command(label=START, command=lambda: btnStart(mes))

    def btnStart(mes):
        ServiceOpt.ServiceStart(mes)
        fileMenu.entryconfig(START, state=DISABLED)
        printLabel(tk, STATE_STARTING, count, 4, 1)

    # 停止服务
    fileMenu.add_command(label=STOP, command=lambda: btnStop(mes))

    def btnStop(mes):
        ServiceOpt.ServiceStop(mes)
        fileMenu.entryconfig(STOP, state=DISABLED)
        printLabel(tk, STATE_STOPPING, count, 4, 1)

    # 重启服务
    fileMenu.add_command(label=RE_START, command=lambda: btnReStart(mes))

    def btnReStart(mes):
        ServiceOpt.ServiceReStart(mes)
        fileMenu.entryconfig(RE_START, state=DISABLED)
        printLabel(tk, STATE_RE_STARTING, count, 4, 1)

    fileMenu.add_separator()

    # 打开服务日志
    fileMenu.add_command(label=LOG_FILE,
                         command=lambda: btnLogFile(LogAddress, LogFile))

    def btnLogFile(LogAddress, LogFile):
        ServiceOpt.openNoteByName(LogAddress, LogFile)
        fileMenu.entryconfig(LOG_FILE, state=DISABLED)

    fileMenu.add_separator()

    # 打开服务自启
    fileMenu.add_command(
        label=SET_START_AUTO,
        command=lambda: btnSetState(mes, SET_START_AUTO, "auto"))
    # 关闭服务自启
    fileMenu.add_command(
        label=SET_START_DEMAND,
        command=lambda: btnSetState(mes, SET_START_DEMAND, "demand"))
    # 禁用服务
    fileMenu.add_command(
        label=SET_START_DISABLED,
        command=lambda: btnSetState(mes, SET_START_DISABLED, "disabled"))

    def btnSetState(mes, label, STATE):
        ServiceOpt.ServiceSetStartAuto(mes, STATE)
        fileMenu.entryconfig(label, state=DISABLED)

    fileMenu.add_separator()

    # 安装服务
    fileMenu.add_command(label=INSTALL,
                         command=lambda: btnSetup(SetupAddress, mes))

    def btnSetup(SetupAddress, mes):
        ServiceOpt.openSetup(SetupAddress, mes)
        fileMenu.entryconfig(INSTALL, state=DISABLED)
        printLabel(tk, STATE_INSTALLING, count, 4, 1)

    # 卸载服务
    fileMenu.add_command(label=UNINSTALL, command=lambda: btnDelete(mes))

    def btnDelete(mes):
        ServiceOpt.ServiceDelete(mes)
        fileMenu.entryconfig(UNINSTALL, state=DISABLED)
        printLabel(tk, STATE_UNINSTALLING, count, 4, 1)

    if LogFile == '':
        fileMenu.entryconfig(LOG_FILE, state=DISABLED)

    if FLAG == -1:
        for index in [
                START, STOP, RE_START, SET_START_AUTO, SET_START_DEMAND,
                SET_START_DISABLED, UNINSTALL
        ]:
            fileMenu.entryconfig(index, state=DISABLED)
    elif FLAG == 0:
        fileMenu.entryconfig(STOP, state=DISABLED)
        fileMenu.entryconfig(INSTALL, state=DISABLED)
    elif FLAG == 1:
        fileMenu.entryconfig(START, state=DISABLED)
        fileMenu.entryconfig(INSTALL, state=DISABLED)

    btn.config(menu=fileMenu)
 def btnDelete(mes):
     ServiceOpt.ServiceDelete(mes)
     fileMenu.entryconfig(UNINSTALL, state=DISABLED)
     printLabel(tk, STATE_UNINSTALLING, count, 4, 1)
 def btnSetup(SetupAddress, mes):
     ServiceOpt.openSetup(SetupAddress, mes)
     fileMenu.entryconfig(INSTALL, state=DISABLED)
     printLabel(tk, STATE_INSTALLING, count, 4, 1)
 def btnSetState(mes, label, STATE):
     ServiceOpt.ServiceSetStartAuto(mes, STATE)
     fileMenu.entryconfig(label, state=DISABLED)
def ServiceLogList():
    ServiceOpt.openFile(LogListAddress)
def ServiceAllStop(ServiceNameList):
    for count in range(int(len(ServiceNameList) / 2)):
        ServiceOpt.ServiceStop(ServiceNameList[int(count * 2)])
        ServiceState(count, 3, 1, ServiceNameList[int(count * 2)])
def openFileList(Address):
    ServiceOpt.openFile(Address)
def ServiceAllStop(ServiceNameList):
    for count in range(int(len(ServiceNameList) / 3)):
        ServiceOpt.ServiceStop(ServiceNameList[int(count * 3)])
def ServiceAllStop(ServiceNameList):
    for count in range(len(ServiceNameList)):
        ServiceOpt.ServiceStop(ServiceNameList[count])