实现一个电话号码生成器

任务背景

之前在测试一个系统的时候,经常需要用到手机号码,但是一个手机号使用后就不能再次使用了,所以经常要想一些可用的手机号,如18888888888等等,每次想手机号也挺麻烦的,所以这次想着做一个生成手机号的小工具。

实现过程

1、基本实现

import random

list_1 = ["134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172", "178",
          "182", "183", "184", "187", "188", "198"]  # 中国移动号码段

list_2 = ["130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186"]  # 中国联通号码段

list_3 = ["133", "149", "153", "173", "177", "180", "181", "189", "191", "199", "193"]  # 中国电信号码段

num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]  # 存放0-9数字,号码的4-11位从这里取

phone_all = list()  # 存放所有生成的电话号码
phone_output = list() # 存放去重后的电话号码


def create_phone(count, choice): # 参数1为生成号码的个数,参数2为运营商选择
    for t in range(count):
        phone = random.choice(choice) + "".join(random.choice(num) for i in range(8))  #使用random函数生成电话号码
        if phone not in phone_all:  # 判断该电话号码是不是出现过
            phone_output.append(phone)  # 没出现则放到phone_output
        phone_all.append(phone) # 把生成的每个号码都存起来,用去去重比对
    print(phone_output)  # 打印去重后的电话


if __name__ == '__main__':
    create_phone(10, list_3)

2、使用Tkinter做一个界面小工具

代码如下

# -*- coding:utf-8 -*-

import tkinter as tk
from tkinter import ttk
from tkinter import *
import random


class Phone():
    def __init__(self):
        self.window = tk.Tk()  # 创建window窗口
        self.window.title("手机号码生成器")  # 定义窗口名称
        # self.window.resizable(0,0)  # 禁止调整窗口大小
        self.menu = ttk.Combobox(self.window, width=6)
        self.path = StringVar()
        # self.lab1 = tk.Label(self.window, text="目标路径:")
        self.lab2 = tk.Label(self.window, text="选择运营商:")
        self.lab3 = tk.Label(self.window, text="生成数量:")
        self.count = tk.Entry(self.window, width=5)
        self.info = tk.Text(self.window, height=20)  # 创建一个文本展示框,并设置尺寸

        self.menu['value'] = ('中国联通', '中国移动', '中国电信')
        self.menu.current(0)

        # 添加一个按钮,用于触发生成号码
        self.t_button1 = tk.Button(self.window, text='生成号码', relief=tk.RAISED, width=8, height=1,
                                   command=self.create_phone)
        # 添加一个按钮,用于触发清空输出框功能
        self.c_button2 = tk.Button(self.window, text='清空输出', relief=tk.RAISED, width=8, height=1, command=self.cle)

    def gui_arrang(self):
        """完成页面元素布局,设置各部件的位置"""
        # self.lab1.grid(row=0, column=0)
        self.lab2.grid(row=0, column=0)  # 选择运营商标题按钮位置
        self.menu.grid(row=0, column=1, sticky=W)  # 选择运营商下拉框位置
        self.lab3.grid(row=1, column=0)  # 生成数量标题位置
        self.count.grid(row=1, column=1, sticky=W)  # 生成数量输入框位置
        self.info.grid(row=2, rowspan=5, column=0, columnspan=3, padx=15, pady=15)  # 展示结果文本框位置

        self.t_button1.grid(row=0, column=2)  # 生成号码按钮位置
        self.c_button2.grid(row=1, column=2)  # 清空输出按钮

    def get_choice(self):
        category = {
            'list_1': ["134", "135", "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172",
                       "178",
                       "182", "183", "184", "187", "188", "198"],  # 中国移动号码段
            'list_2': ["130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186"],  # 中国联通号码段
            'list_3': ["133", "149", "153", "173", "177", "180", "181", "189", "191", "199", "193"]  # 中国电信号码段
        }
        cid = None
        if self.menu.get() == "中国联通":
            cid = category["list_1"]
        elif self.menu.get() == "中国移动":
            cid = category["list_2"]
        elif self.menu.get() == "中国电信":
            cid = category["list_3"]
        return cid

    @staticmethod
    def basic_num():
        num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
        return num

    def create_phone(self):
        phone_all = list()  # 存放所有生成的电话号码
        phone_output = list()  # 存放去重后的电话号码
        for t in range(int(self.count.get())):
            phone = random.choice(self.get_choice()) + "".join(random.choice(self.basic_num()) for i in range(8))
            if phone not in phone_all:
                phone_output.append(phone)  # 判断电话号码是不是出现过,没出现就追加到phone_output中
                # phone_output = "".join(phone)
            phone_all.append(phone) # 把生成的每一个号码都追加到phone_all(去重参考物)
            # phone_all = "".join(phone)
        # print(phone_output)

        step = 6  # 设置一个值,每次显示6个号码
        for b in [phone_output[i:i + step] for i in range(0, len(phone_output), step)]:  # 每次打印6个号码
            print(",".join(b))  # 把列表中的号码取出来并以","隔开,形式是字符串
            self.info.insert('end', ",".join(b) + '\n')  # 输出到页面,并且每输出一组(6个)就追加一个换行符

    def cle(self):
        """定义一个函数,用于清空输出框的内容"""
        self.info.delete(1.0, "end")  # 从第一行清除到最后一行


def main():
    t = Phone()
    t.gui_arrang()
    tk.mainloop()


if __name__ == '__main__':
    main()

界面如下:

 遇到的问题

因为开始输出到界面的时候是按照显示框的大小自动换行的,所以有时会遇到一个号码分两行显示,为了解决这个问题,考虑如下:
(1)每次只输出6个电话号码,也就是6个为一组

(2)输出一组后,紧接着输出一个换行符

关于第一点,在网上搜索了一下,如何把一个列表中的数据按照一定数量分组输出,方法如下

参考博客:https://blog.csdn.net/Mr_Cat123/article/details/80584988
a = [1,2,3,4,5,6,7,8,9,10,11]
step = 3
b = [a[i:i+step] for i in range(0,len(a),step)]
print(b)

>>>[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
>>> b[1]
[4, 5, 6]
上面的step是指每次输出3个数字

关于第二点,开始是在每个列表后追加一个换行符,发现每次显示到界面时,都会显示一个{}

 后来想着可能是数据格式的问题,不能把换行符加到列表中,然后处理了一下,先把列表转换成字符(使用join()方法),然后在每组字符后追加一个换行符,如下

猜你喜欢

转载自www.cnblogs.com/hanmk/p/11520110.html