[Python] EasyGui implements an interactive program for extracting lucky audience

Table of contents

        I met a relatively simple class assignment, and I knew every word of the words "user interface friendly", but when they were connected together, I didn't know exactly what the teacher wanted, so I could only follow my own The idea came to understand.

        The specific requirements for major assignments are as follows:

        "Friendly user interface" , according to my idea, it can only start to appear a pop-up window, and then give three options (input, extract, exit), which button to press which one to operate.

       If you select the input button, the input interface will pop up, with three information input boxes (number, name, mobile phone number), or two, and the number will be automatically incremented.

       If you choose to draw lucky spectators, then randomly select three lucky spectators, one at a time, and pop up a window to confirm whether the drawn people are present, register information if they are present, otherwise draw again.

       Repeatedly emphasize the exit button, click to exit the program.


 1. What is EasyGUI?

        In easygui, all GUI interactions are invoked by simple function calls.

        This is a simple demo program using easygui. The screen it generates is displayed on the easygui home page.

while 1 :  
    msgbox ( "Hello, world!" )  

    msg ​​= "What's your favorite flavor?"  
    title = "Ice Cream Survey"  
    Choices = [ "Vanilla" , "Chocolate" , "Strawberry" , "Rocky Road" ]  
    choice = choicebox ( msg , title , choices )  

    # Note that we convert the choices to strings, just in case 
    the user deselects it, we get None. 
    msgbox ( "You have chosen: " " choice ), "Survey Result" )  

    msg​​="Do you want to continue? 
    title = "Please confirm"  
    if ccbox( msg , title ): # show continue/cancel dialog 
        pass # user chooses to continue 
    else:  
        sys.exit(0) # user chooses to cancel

2. Use steps

1. Import library

import easygui

2. Improve the main function

        Because when the information is entered into the file later, it is appended to the file, so the original information in the file is cleared at the beginning.

if __name__ == '__main__':
    #清除文件信息
    f = open("file05.txt", "w")
    f.close()

3. Display the action box


    while True:
        choice = easygui.indexbox('请选择下面的操作', '抽取幸运观众', ('录入观众信息', '抽取幸运观众','退出'))

        The specific effect is like this.

4. Increase the operation of entering information

#录入观众的信息
def registerInfo(number,name,phone):
    person = [number, name, phone]
    #查找观众是否已经存在
    if audiencesInfo.count(person) == 0:
        audiencesInfo.append(person)
        f = open("file05.txt", "a")
        f.write('\n')
        f.writelines(' '.join(person))
        f.close()
    else:
        return False
    return True
        if choice == 0:
            filedNames = ['编号','姓名','手机号码']
            number,name,phone = easygui.multenterbox('输入观众信息','输入观众信息', filedNames)
            value = registerInfo(number,name,phone)
            if value:
                easygui.msgbox('录入成功')
            else:
                easygui.msgbox('观众已存在')
            #print(audiencesInfo)

5. Increase the function of drawing lucky viewers

def drawAudience():
    flag = True
    random_count = 0
    address = ''
    while flag:
        lucklyNum = random.randint(0, len(audiencesInfo) - 1)
        if lucklyIndex.count(lucklyNum)==0:
            address = signInPerson(lucklyNum)
            if len(address) > 0:
                flag = False
        random_count += 1
        #限制随机次数,防止出现三位观众,有一位没有到,导致一直随机的问题
        if random_count > 3*len(audiencesInfo):
            return False
    lucklyAudiences.append([audiencesInfo[lucklyNum][0],audiencesInfo[lucklyNum][1],audiencesInfo[lucklyNum][2],address])
    #print(lucklyAudiences)
    return True

The following function is to register the audience information

def signInPerson(lucklyNum):
    msg = str(audiencesInfo[lucklyNum][1]) + str(" ") + str(audiencesInfo[lucklyNum][2]) + str("是否到场")
    value = easygui.ynbox(msg)
    if value:
        lucklyIndex.append(lucklyNum)
        address = easygui.enterbox('登记地址')
        return address
    else:
        return ''

This is to save the lucky audience information

def saveLuckly():
    fw = open("luckly.txt", "w")
    for personInfo in lucklyAudiences:
        fw.writelines('  '.join(personInfo))
        fw.write('\n')
    fw.close()

The main function handles option manipulation

        elif choice == 1:
            if len(audiencesInfo)>=3:
                lucklyIndex.clear()
                lucklyAudiences.clear()
                for i in range(0,3):
                    value = drawAudience()
                    if value is not True:
                        easygui.msgbox('抽取幸运观众失败,请检查到场观众位数')
                        break
                if len(lucklyAudiences)==3:
                    saveLuckly()
            else:
                easygui.msgbox('观众数量不足三位,请补充信息')

 

6. Exit function

        elif choice == 2:
            exit()
        else:
            #防止增加输入框但没有改上面对应的数字导致无法退出
            exit()

The complete code is as follows:

#!/usr/bin/python3
import random
import easygui

#录入的观众信息
#audiencesInfo = [['0','张三','110'],['1','张四','119'],['2','张五','120']]
audiencesInfo = []

#抽取的幸运观众
lucklyIndex = []
lucklyAudiences = []

#录入观众的信息
def registerInfo(number,name,phone):
    person = [number, name, phone]
    #查找观众是否已经存在
    if audiencesInfo.count(person) == 0:
        audiencesInfo.append(person)
        f = open("file05.txt", "a")
        f.write('\n')
        f.writelines(' '.join(person))
        f.close()
    else:
        return False
    return True

def drawAudience():
    flag = True
    random_count = 0
    address = ''
    while flag:
        lucklyNum = random.randint(0, len(audiencesInfo) - 1)
        if lucklyIndex.count(lucklyNum)==0:
            address = signInPerson(lucklyNum)
            if len(address) > 0:
                flag = False
        random_count += 1
        #限制随机次数,防止出现三位观众,有一位没有到,导致一直随机的问题
        if random_count > 3*len(audiencesInfo):
            return False
    lucklyAudiences.append([audiencesInfo[lucklyNum][0],audiencesInfo[lucklyNum][1],audiencesInfo[lucklyNum][2],address])
    #print(lucklyAudiences)
    return True

def saveLuckly():
    fw = open("luckly.txt", "w")
    for personInfo in lucklyAudiences:
        fw.writelines('  '.join(personInfo))
        fw.write('\n')
    fw.close()

def signInPerson(lucklyNum):
    msg = str(audiencesInfo[lucklyNum][1]) + str(" ") + str(audiencesInfo[lucklyNum][2])
    msg1 = msg + str("是否到场")
    value = easygui.ynbox(msg1)
    if value:
        lucklyIndex.append(lucklyNum)
        msg2 = msg+str("登记地址")
        address = easygui.enterbox(msg2)
        return address
    else:
        return ''

# def signIn():
#     choices = []
#     choices.append(lucklyAudiences[0][1:3])
#     choices.append(lucklyAudiences[1][1:3])
#     choices.append(lucklyAudiences[2][1:3])
#     selectPerson = easygui.multchoicebox('确认幸运观众是否到场', '签到', choices)


if __name__ == '__main__':
    #清除文件信息
    f = open("file05.txt", "w")
    f.close()

    while True:
        choice = easygui.indexbox('请选择下面的操作', '抽取幸运观众', ('录入观众信息', '抽取幸运观众','退出'))
        if choice == 0:
            filedNames = ['编号','姓名','手机号码']
            number,name,phone = easygui.multenterbox('输入观众信息','输入观众信息', filedNames)
            value = registerInfo(number,name,phone)
            if value:
                easygui.msgbox('录入成功')
            else:
                easygui.msgbox('观众已存在')
            #print(audiencesInfo)
        elif choice == 1:
            if len(audiencesInfo)>=3:
                lucklyIndex.clear()
                lucklyAudiences.clear()
                for i in range(0,3):
                    value = drawAudience()
                    if value is not True:
                        easygui.msgbox('抽取幸运观众失败,请检查到场观众位数')
                        break
                if len(lucklyAudiences)==3:
                    saveLuckly()
            else:
                easygui.msgbox('观众数量不足三位,请补充信息')
        elif choice == 2:
            exit()
        else:
            #防止增加输入框但没有改上面对应的数字导致无法退出
            exit()

Summarize

        EasyGui is also a very simple and easy-to-use library. Basically, it only takes one line of code to implement buttons, input boxes, and selection boxes.

Guess you like

Origin blog.csdn.net/xanadw/article/details/124952759