模块
import tkinter as tk
import re
from PIL import Image, ImageTk
import requests
from bs4 import BeautifulSoup
import base64
import io# 窗口
root = tk.Tk()
root.title("封禁IP及其归属地查询")
def center_window(window):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window.update_idletasks()
window_width = window.winfo_reqwidth()
window_height = window.winfo_reqheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
图片区
base64_strings = {
#logo的base编码,详细代码在txt文件内
"huawei_logo": "<iVBOR.....>",
"hillstone": "<iVBOR.....>",
"h3c_logo": "<iVBOR.....>",
"52pojie": "<iVBOR.....>"
}
def create_image_label(root, photo_base64, row, column, sticky):
base64_strings = base64.b64decode(photo_base64)
image= Image.open(io.BytesIO(base64_strings))
photo = ImageTk.PhotoImage(image)
label = tk.Label(root, image=photo)
label.grid(row=row, column=column, sticky=sticky)
label.photo = photo
按钮触发IP操作
def gettext():
content = huawei.get("1.0", "100.end")
ip = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', content)
# 清空所有窗口内容
huawei.delete('1.0', "100.end")
hillstone.delete('1.0', "100.end")
h3c.delete('1.0', "100.end")
belong.delete('1.0', "100.end")
# 输入窗口内容
hillstone.insert(tk.INSERT, "config\naddress blacklist\n")
huawei.insert(tk.INSERT, "sys" + "\n")
h3c.insert(tk.INSERT, "sys" + "\n")
# 窗口文本框获取IP
def get_ip():
url = f"http://www.jsons.cn/ipbatch/"
data = {'txt_ip': ip}
response = requests.post(url, data=data)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
table = soup.find("table", {"class": "table-bordered"})
rows = table.find_all("tr")
for j in range(1, len(rows)):
srcip = rows[j].find_all("td")[0].text.strip()
country = rows[j].find_all("td")[1].text.strip()
province = rows[j].find_all("td")[2].text.strip()
city = rows[j].find_all("td")[3].text.strip()
display = f"{srcip} {country} {province} {city}"
belong.insert(tk.INSERT, display + "\n")
else:
belong.insert(tk.INSERT, "查询失败")
for i in ip:
huawei.insert(tk.INSERT, f"firewall blacklist item source-ip {i}\n")
hillstone.insert(tk.INSERT, f"ip {i}/32\n")
h3c.insert(tk.INSERT, f"blacklist ip {i}\n")
get_ip()
文本框区域
huawei = tk.Text(root, height=20, width=50)
huawei.grid(row=1, column=0)
hillstone = tk.Text(root, height=20, width=50)
hillstone.grid(row=1, column=1)
h3c = tk.Text(root, height=20, width=50)
h3c.grid(row=3, column=0)
belong = tk.Text(root, height=20, width=50)
belong.grid(row=3, column=1)
显示窗口
create_image_label(root, "huawei_logo.png", 0, 0, "w")
create_image_label(root, "hillstone.png", 0, 1, "e")
create_image_label(root, "h3c_logo.png", 2, 0, "w")
create_image_label(root, "52pojie.jpg", 2, 1, "e")
center_window(root)
tk.Button(root, text="吾爱出品-https://www.52pojie.cn/", width=40, height=2 ,command=gettext, bg="#CCCCFF").grid(row=2, column=0, columnspan=2)
root.mainloop()
[最终采用评论老哥@VanYun只把图片进行base64转码来规避这个问题]请教:pyinstaller能否把图片也一起封装单文件内,小弟自己试了几次一直不行,显示找不到我的图片,最后只能把图片跟exe文件放置同目录,如果有大佬知道怎么做到的话,还望指点,谢谢!