有没有办法将这个代码组合成一个函数?

时间:2017-11-06 10:45:53

标签: python

  

此代码用于用户配置办公室的开闭时间,4个功能可增加或减少开启或关闭时间。

This is how it looks on the dashboard

closehour = 17
closeminute = 0
openhour = 8
openminute = 0


def increaseOpenTimeScript():
    global openhour
    global openminute
    if openhour == 23 and openminute == 50:
        openhour = 0
        openminute = 0
        time = "{}:{}0".format(openhour,openminute)
    elif openminute == 50:
        openminute = 0
        openhour+=1
        time = "{}:{}0".format(openhour, openminute)
    else:
        openminute+= 10
        time = "{}:{}".format(openhour, openminute)
    office.office1.officeOpenValue.config(text=time)
  

这是其中一个功能,还有3个更像是减少开放时间,增加关闭时间和减少关闭时间。   我想将所有这些代码组合成一个函数,以便更改。

以下是所有功能:

def decreaseOpenTimeScript():
    global openhour
    global openminute
    if openminute >= 10:
        openminute -= 10
        if openminute == 0:
            time = "{}:{}0".format(openhour, openminute)
        else:
            time = "{}:{}".format(openhour,openminute)
    elif openhour > 0 and openminute == 0:
        openminute = 50
        openhour -= 1
        time = "{}:{}".format(openhour,openminute)
    elif openhour == 0 and openminute == 0:
        openhour = 23
        openminute = 50
       time = "{}:{}".format(openhour, openminute)
    office.office1.officeOpenValue.config(text=time)


def increaseCloseTimeScript():
    global closehour
    global closeminute
    if closehour == 23 and closeminute == 50:
        closehour = 0
        closeminute = 0
        time = "{}:{}0".format(closehour,closeminute)
    elif closeminute == 50:
        closeminute = 0
        closehour+=1
        time = "{}:{}0".format(closehour,closeminute)
    else:
        closeminute+= 10
        time = "{}:{}".format(closehour,closeminute)
    office.office1.officeCloseValue.config(text=time)


def decreaseCloseTimeScript():
    global closehour
    global closeminute
    if closeminute >= 10:
        closeminute -= 10
        if closeminute == 00:
            time = "{}:{}0".format(closehour,closeminute)
        else:
            time = "{}:{}".format(closehour, closeminute)
    elif closehour > 0 and closeminute == 0:
        closehour-=1
        closeminute = 50
        time = "{}:{}".format(closehour,closeminute)
    elif closehour == 0 and closeminute == 0:
        closehour = 23
        closeminute = 50
        time = "{}:{}".format(closehour, closeminute)
    office.office1.officeCloseValue.config(text=time)

1 个答案:

答案 0 :(得分:0)

创建一个类并在该类中定义方法会更容易吗?然后,您可以使用您的类来执行每个功能。