首页 > 编程语言 >Python tkinter 制作文章搜索软件,有没有方便快捷不知道,好玩就行了

Python tkinter 制作文章搜索软件,有没有方便快捷不知道,好玩就行了

时间:2022-10-08 15:46:13浏览次数:82  
标签:search tkinter title Python text tree tk 方便快捷 view

前言

无聊的时候做了一个搜索文章的软件,有没有更加的方便快捷不知道,好玩就行了

环境使用

  • Python 3.8
  • Pycharm

模块使用

  • import requests
  • import tkinter as tk
  • from tkinter import ttk
  • import webbrowser

最终效果

对于本篇文章有疑问的同学可以加【资料白嫖、解答交流群:753182387】

界面实现代码

导入模块

import tkinter as tk
from tkinter import ttk

创建窗口

root = tk.Tk()
root.title('问题搜索')
root.geometry('900x700+100+100')
root.iconbitmap('search.ico')

root.mainloop()

标题图片

img = tk.PhotoImage(file='封面.png')
tk.Label(root, image=img).pack()

搜索框

search_frame = tk.Frame(root)
search_frame.pack(pady=10)
search_va = tk.StringVar()
tk.Label(search_frame, text='问题描述:', font=('黑体', 15)).pack(side=tk.LEFT, padx=5)
tk.Entry(search_frame, relief='flat', width=30, textvariable=search_va).pack(side=tk.LEFT, padx=5, fill='both')
tk.Button(search_frame, text='搜索一下', font=('黑体', 12), relief='flat', bg='#fe6b00').pack(side=tk.LEFT,padx=5)

内容显示界面

tree_view = ttk.Treeview(root, show="headings")

tree_view.column('num', width=1, anchor='center')
tree_view.column('title', width=150, anchor='w')
tree_view.column('author', width=10, anchor='center')
tree_view.column('date', width=10, anchor='center')
tree_view.column('link', width=30, anchor='center')
tree_view.heading('num', text='序号')
tree_view.heading('title', text='标题')
tree_view.heading('author', text='作者')
tree_view.heading('date', text='发布时间')
tree_view.heading('link', text='链接')

tree_view.pack(fill=tk.BOTH, expand=True, pady=5)

内容效果代码

def search(word):
    search_list = []
    num = 0
    for page in range(1, 4):
        url = 'https://so.csdn.net/api/v3/search'
        data = {
            'q': word,
            't': 'all',
            'p': page,
            's': '0',
            'tm': '0',
            'lv': '-1',
            'ft': '0',
            'l': '',
            'u': '',
            'ct': '-1',
            'pnt': '-1',
            'ry': '-1',
            'ss': '-1',
            'dct': '-1',
            'vco': '-1',
            'cc': '-1',
            'sc': '-1',
            'akt': '-1',
            'art': '-1',
            'ca': '-1',
            'prs': '',
            'pre': '',
            'ecc': '-1',
            'ebc': '-1',
            'urw': '',
            'ia': '1',
            'dId': '',
            'cl': '-1',
            'scl': '-1',
            'tcl': '-1',
            'platform': 'pc',
        }
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
        }
        response = requests.get(url=url, params=data, headers=headers)
        for index in response.json()['result_vos']:
            title = index["title"].replace('<em>', '').replace('</em>', '')
            dit = {
                'num': num,
                'title': title,
                'author': index['nickname'],
                'date': index['create_time_str'],
                'link': index['url'],
            }
            num += 1
            search_list.append(dit)
    return search_list


def show(search_list):
    # 往树状图中插入数据
    for index, stu in enumerate(search_list):
        tree_view.insert('', index + 1,
                         values=(stu['num'], stu['title'], stu['author'], stu['date'], stu['link']))


def click():
    key_word = search_va.get()
    if key_word:
        search_list = search(word=key_word)
        # 往树状图中插入数据
        show(search_list)

# 单击 获取当前点击行的值
def tree_view_click(event):
    # 遍历选中的元素
    for item in tree_view.selection():
        # 获取选中元素的值
        item_text = tree_view.item(item, "values")
        # 打印选中元素的值
        # print(item_text)
        webbrowser.open(item_text[-1])

标签:search,tkinter,title,Python,text,tree,tk,方便快捷,view
From: https://www.cnblogs.com/qshhl/p/16769135.html

相关文章

  • Python多线程
    一、概念线程是CPU分配资源的基本单位,当程序开始运行,这个程序就变成了一个进程;当有多线程编程时,一个进程包含多个线程(含主线程),使用线程可以实现程序大的开发任务。多线......
  • python -list赋值给变量-读取list中数据
    常规读取list中数据#使用索引list=[1,2,3]a=list[1]#循环遍历foriinlist: print(i)快速赋值a,b,c=list#这种方式只有当左边的操作数个数和list长度相同,也......
  • tkinter的几个例子
    参考这个网站学习TkinterTextAddScrollbardesign|PythonGUITutorial-ApiDemos™ 第一个简单点frompathlibimportPath#importos#fromtimeimporttim......
  • 【Python小工具】爬虫之获取图片验证码
    Python小工具系列是一个使用Python实现各种各样有意思的小玩意儿的系列,包括制作个性化的二维化、词云、简单爬虫等,持续更新中,如果你感兴趣就关注一波吧!一、基本介绍接上一篇......
  • 【Python小工具】爬虫之使用OpenCV识别数字+字母验证码详解,告别收费
    Python小工具系列是一个使用Python实现各种各样有意思的小玩意儿的系列,包括制作个性化的二维化、词云、简单爬虫等,持续更新中,如果你感兴趣就关注一波吧!一、基本介绍......
  • python 报错“UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte“的解决办法
    用python读取一个txt文件withopen(file,encoding='utf-8')asf:conlines=f.readlines()运行报错:UnicodeDecodeError:‘utf-8’codeccan’tdecode......
  • python
    1    2    3    4    5    6    7    8    9    10    实战1   ......
  • python-列表list--[ ]
    创建列表List=['wade','james','bosh','haslem']可以创建空列表list添加新元素List.append('allen')方式一:向list结尾添加参数objecta=[1,2,3,4]a.......
  • 这8个技巧直接让你的Python性能起飞
    1.使用map()进行函数映射✅Exp1:将字符串数组中的小写字母转为大写字母。测试数组为oldlist=['life','is','short','i','choose','python']。 方法一ne......
  • 【Web开发】Python实现Web服务器(web2py)
    文章目录​​1、简介​​​​2、下载和安装​​​​3、快速入门​​​​4、示例测试​​​​4.1Sayhello​​​​4.2Let'scount​​​​4.3Saymyname​​​​4.4Pos......