python GUI入门小工具开发 用来练练手
效果图
1、创建一个界面 显示对应的系统配置信息
2、人工还是自动获取本机信息,这里采用的是人工点击获取
3、获取完配置信息后,点击提交并退出软件,数据之间插入至数据库。字段不够自己加就行了
import uuid
不废话直接上代码
#导入库
import socket
import pymssql
from tkinter import *
from tkinter import messagebox
import datetime
from wmi import WMI
class Ip_Mac():
def __init__(self):
self.window = Tk()
self.window.title('HOSTNAME_MAC_IP') #定义窗口名称
self.juzhong() #设置一个窗口居中函数
self.window.resizable(0,0) #固定窗口大小
self.over_time() # 设置过期时间
# 文本列表名
self.lab1 = Label(self.window, text='赛亚名*:', font=('微软雅黑'),fg='red') # 赛亚名
self.lab2 = Label(self.window, text='MAC地址:', font=('微软雅黑')) # MAC
self.lab3 = Label(self.window, text='IP地址:', font=('微软雅黑')) # IP地址
self.lab4 = Label(self.window, text='主机名:', font=('微软雅黑')) # 主机名
self.lab5 = Label(self.window, text='填好姓名一键获取配置信息\n点击"提交"即可', font=('微软雅黑'), fg='red') # 标签
self.lab6 = Label(self.window, text='抖音搜索"白嫖的IT知识"获取更多实用小工具', font=('微软雅黑'), fg='green') # 标签
self.lab7 = Label(self.window, text='CPU参数:', font=('微软雅黑')) # CPU参数
self.lab8 = Label(self.window, text='核心数:', font=('微软雅黑')) # CPU参数
self.lab9 = Label(self.window, text='内存1参数:', font=('微软雅黑')) # 内存1参数
self.lab10 = Label(self.window, text='内存2参数:', font=('微软雅黑')) # 内存2参数
self.lab11 = Label(self.window, text='内存1品牌:', font=('微软雅黑')) # 内存1品牌
self.lab12 = Label(self.window, text='内存2品牌:', font=('微软雅黑')) # 内存2品牌
self.lab13 = Label(self.window, text='硬盘1容量:', font=('微软雅黑')) # 硬盘1
self.lab14 = Label(self.window, text='硬盘2容量:', font=('微软雅黑')) # 硬盘2
self.lab15 = Label(self.window, text='显卡1参数:', font=('微软雅黑')) # 显卡1参数
self.lab16 = Label(self.window, text='显卡2参数:', font=('微软雅黑')) # 显卡2参数
#文本框
self.entry_name = Entry(self.window, width=22) #名称
self.entry_mac = Entry(self.window, width=22) # readonly状态不可编辑 MAC
self.entry_Ip = Entry(self.window, width=22) # IP
self.entry_host = Entry(self.window, width=22) # 主机名
self.entry_cpu = Entry(self.window, width=40) # cpu
self.entry_cpu_hx = Entry(self.window, width=7) # CPU核心数
self.entry_memory_pp1 = Entry(self.window, width=22) # 内存1品牌
self.entry_memory1 = Entry(self.window, width=22) # 内存1
self.entry_memory_pp2 = Entry(self.window, width=22) # 内存1品牌
self.entry_memory2 = Entry(self.window, width=22) # 内存2
self.entry_Disk1 = Entry(self.window, width=22) # 硬盘1
self.entry_Disk2 = Entry(self.window, width=22) # 硬盘2
self.entry_video1 = Entry(self.window, width=22)
self.entry_video2 = Entry(self.window, width=22) # 显卡2参数
# 按钮
self.but3 = Button(self.window, text='提交', relief=RAISED, bg='yellow', font=('微软雅黑', '16', 'bold'),
command=self.get_mac) #点击提交 提交至数据库
self.but4 = Button(self.window, text='一键获取本机配置信息', relief=RAISED, bg='yellow', font=('微软雅黑', '16', 'bold'),
command=self.get_cpu) #点击提交 提交至数据库
def juzhong(self):
width = 495
height = 280
self.window.geometry(f'{width}x{height}') #设计的窗口大小
#居中位置 计算屏幕的居中位置
screen_width = self.window.winfo_screenwidth() / 2 - width / 2
screen_height = self.window.winfo_screenheight() / 2 - height / 2
self.window.geometry(f"+{int(screen_width)}+{int(screen_height)}") #偏移位置
#设置过期时间
def over_time(self):
over_time = '2022-09-17'
now_time = datetime.datetime.now().strftime('%Y-%m-%d')
if now_time > over_time:
messagebox.showerror('错误','已过期,抖音搜索"白嫖的IT知识"获取更新版')
self.window.destroy()
def get_mac(self):
user_list = []
insert_time = datetime.datetime.now()
if self.entry_name.get() == '':
messagebox.showerror('提示','赛亚名不能为空')
else:
name = self.entry_name.get() #赛亚名
# mac = uuid.UUID(int=uuid.getnode()).hex[-12:] #MAC
# self.entry_mac.insert(END,mac)
# hostname = socket.gethostname() #主机名
# ip = socket.gethostbyname(hostname) #IP地址
#链接数据库 sql server
conn = pymssql.connect(
host='',
user='sc',
password='',
database='TestServer',
charset='UTF-8'
)
# 创建游标
cursor = conn.cursor()
select_info = "select * from py_ip_address"
cursor.execute(select_info)
result = cursor.fetchall()
# print(result)
for i in result:
user_list.append(i[0])
# print(i)
if name in user_list:
messagebox.showerror('错误', '赛亚名已存在')
elif self.entry_Ip.get() == '':
messagebox.showerror('错误', '未点击“一键获取本机配置信息”')
else:
# 写sql
insert_sql = f"insert into py_ip_address(name,mac,hostname,IP,create_time) values ('{name}','{self.mac}','{self.hostname}','{self.ip}','{insert_time}')"
print(insert_sql)
# 执行sql
cursor.execute(insert_sql)
# 提交数据
conn.commit()
messagebox.showinfo('提示','提交成功')
self.window.destroy() #关闭程序