超酷的爬虫可视化界面

大家好,本文主要介绍使用tkinter获取本地文件夹、设置文本、创建按钮下拉框和对界面进行布局。

1.导入tkinter库

导入tkinter的库,可以使用ttkbootstrap美化生成的界面

ttkbootstrap官网地址:https://ttkbootstrap.readthedocs.io/en/latest/zh/styleguide/frame/

python 复制代码
import tkinter as tk
from ttkbootstrap.constants import *
from tkinter.filedialog import askdirectory
from ttkbootstrap import Frame, Button, Label, Text, Entry, Combobox

2.窗口属性

主要内容包括:

  • 根据设备屏幕的宽高自适应调整窗口的宽高

  • 设置窗口打开的位置

  • 设置窗口标题

  • 创建图片保存地址、输入关键词、下载数量的变量

  • 创建两个矩形区域包含组件

python 复制代码
class BaiDuImageSpiderGUI:
    def __init__(self, win_width=1000, win_height=600):
        # 创建一个 tkinter 对象
        self.root = tk.Tk()
        # 设置窗口的宽度和高度
        self.win_width = win_width
        self.win_height = win_height
        # 设备屏幕的宽度和高度
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        # 设置窗口打开的x,y位置
        x = int((screen_width - win_width) / 2)
        y = int((screen_height - win_height) / 2)
        # 窗口标题
        self.root.title("图片爬虫")
        self.root.geometry("%sx%s+%s+%s" % (win_width, win_height, x, y))
        # 设置窗口宽高固定
        # self.root.resizable(None,None)
        # 设置窗口图标
        # root.iconbitmap("./image/icon.ico")
        # 下载数量
        self.download_num = tk.StringVar()
        # 保存图片地址
        self.save_image_path = tk.StringVar()
        # 关键词内容
        self.key_word = tk.StringVar()
        # 第一个矩形区域
        self.frame_1 = Frame(self.root)
        self.frame_1.pack()
        # 第一个矩形区域
        self.frame_2 = Frame(self.root)
        self.frame_2.pack()

3.按钮和输入框

主要内容包括:

  • 在第一个矩形框中绘制组件

  • 用Label组件设置文本

  • 用Button和askdirectory组件打开文件夹

  • 用Entry组件绘制输入框

  • 用Button组件绘制按钮

  • 用Combobox组件绘制下拉框

  • 用pack方法设置组件位置

python 复制代码
def create_frame_1(self):
    # 图片保存地址
    image_path_label = Label(self.frame_1, text="图片保存地址:")
    image_path_label.pack(side=LEFT, padx=5, pady=10)
    # 图片地址输入框
    image_path_entry = Entry(self.frame_1, bootstyle=SUCCESS, textvariable=self.save_image_path)
    image_path_entry.pack(side=LEFT, padx=5, pady=10)
    # 路径选择按钮
    image_path_button = Button(self.frame_1, text="路径选择", bootstyle=SUCCESS, command=self.save_path)
    image_path_button.pack(side=LEFT, padx=5, pady=10)
    # 关键字输入框
    key_word_label = Label(self.frame_1, text="图片内容:")
    key_word_label.pack(side=LEFT, padx=5, pady=10)
    key_word_entry = Entry(self.frame_1, bootstyle=INFO, textvariable=self.key_word)
    key_word_entry.pack(side=LEFT, padx=5, pady=10)
    # 下拉框描述
    combox_label = Label(self.frame_1, text="下载数量:")
    combox_label.pack(side=LEFT, padx=5, pady=10)
    # 下拉框绑定到tinker专属的变量上
    combox = Combobox(self.frame_1, bootstyle=PRIMARY, textvariable=self.download_num)
    # 设置下拉框的值、权限、默认值
    combox["value"] = (10, 20, 30, 40, 50)
    # combox["state"] = "readonly"
    combox.current(0)
    # 设置下拉框位置
    combox.pack(side=LEFT, padx=5, pady=10)
    # 按钮
    button = Button(self.frame_1, text="确定", bootstyle=PRIMARY, command=self.spider_main)
    button.pack(side=LEFT, padx=5, pady=10)

4.文本输入框Text

在第二个矩形框中用Text绘制文本输入框,将下载信息写入文本框:

python 复制代码
def create_frame_2(self):
    # # 文本框
    self.download_input_text = Text(self.frame_2, width=self.win_width - 200, height=self.win_height - 200)
    self.download_input_text.pack(side=TOP, anchor=CENTER, padx=50, pady=10)

def save_path(self):
    path_ = askdirectory()
    self.save_image_path.set(path_)

def spider_main(self):
 pass

def create_window(self):
    self.create_frame_1()
    self.create_frame_2()
    self.root.mainloop()

5.运行

实例化BaiDuImageSpiderGUI(),调用create_window()方法创建界面:

python 复制代码
if __name__ == '__main__':
    app = BaiDuImageSpiderGUI()
    app.create_window()

6.打包成exe文件

为了方便使用和运行,使用pyinstaller工具打包为exe文件:

python 复制代码
pyinstaller -F -w -i sspython.ico image_spiderGUI.py
相关推荐
Tummer83635 分钟前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
九章云极AladdinEdu12 分钟前
GPU与NPU异构计算任务划分算法研究:基于强化学习的Transformer负载均衡实践
java·开发语言·人工智能·深度学习·测试工具·负载均衡·transformer
好吃的肘子34 分钟前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
ghost14336 分钟前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
小白学大数据37 分钟前
Scrapy框架下地图爬虫的进度监控与优化策略
开发语言·爬虫·python·scrapy·数据分析
立秋678943 分钟前
用Python绘制梦幻星空
开发语言·python·pygame
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 16课题、包、单元包及模块
开发语言·青少年编程·rust·编程与数学
后青春期的诗go1 小时前
基于Rust语言的Rocket框架和Sqlx库开发WebAPI项目记录(二)
开发语言·后端·rust·rocket框架
咕噜咕噜啦啦2 小时前
python爬虫实战训练
爬虫·python
草莓熊Lotso2 小时前
【C语言字符函数和字符串函数(一)】--字符分类函数,字符转换函数,strlen,strcpy,strcat函数的使用和模拟实现
c语言·开发语言·经验分享·笔记·其他