栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Python程序资源库】编写一个简单的浏览器

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Python程序资源库】编写一个简单的浏览器

Python编写浏览器

前言

由于小赵周末太闲,于是就想用tkinter做个Python GUI开发版浏览器、

代码讲解

运用模块

tkinter-GUI窗口

webbrowser-打开web网页

图标转换网站:PNG转ICO - 免费在线将PNG文件转换成ICO (cdkm.com)

1召唤窗口部分
# -*- coding:utf-8 -*-
import tkinter as tk                   # 导入模块tkinter
import webbrowser as web               # 导入模块webbrowser
a = tk.Tk()                            # 召唤窗口到变量a中
a.geometry("500x500")                  # 将窗口a的大小设为500x500,
a.resizable(True,True)                 # 将窗口a的可伸缩性设为:长:True 宽:True
a.title('燃烧别人,照亮自己-浏览器')     # 将窗口命名为:燃烧别人,照亮自己-浏览器
a.mianloop()                           # 重复刷新窗口,不然会无响应

2输入网页部分

这部分代码要放在a.mainloop()前

input_web = tk.Entry(a)                         # 将input_web变量设为tkinter.Entry单行输入框,安置在窗口a上
input_web.pack()                                # 放置变量input_web
input_web.insert('https://www.microsoft.com')   # 将input_web设置一个初识地址

3获取并打开网址

这部分代码需要写在上部分代码前,mainloop后

def web_go():
    url = input_web.get()
    try:
        web.open(url)
    except:
        from tkinter import messagebox as msg
        msg.showerror('浏览错误', f'浏览器没有查询到域名对应的网站,请确认,错误域名{url}')
go_web = tk.Button(a, text="打开", command=web_go)
go_web.pack()
4完整代码
# -*- coding:utf-8 -*-
"""
by:csdn>方块小赵
"""
import tkinter as tk
import webbrowser as web
a = tk.Tk()
a.geometry("500x500")
a.resizable(True,True)
a.title("燃烧别人,照亮自己-浏览器")
input_web = tk.Text(a, width=50, height=1)
input_web.pack()
input_web.insert('insert', 'https://www.microsoft.com')
def web_go():
    url = input_web.get('1.0', 'end')
    try:
        web.open(url)
    except:
        from tkinter import messagebox as msg
        msg.showerror('浏览错误', f'浏览器没有查询到域名对应的网站,请确认,错误域名{url}')
go_web = tk.Button(a, text="打开", command=web_go)
go_web.pack()
a.mainloop()
5运行结果 6升级

 

 

# -*- coding:utf-8 -*-
import tkinter as tk
import webbrowser as web
from PIL import Image
from PIL import ImageTk
a = tk.Tk()
a.geometry("400x300")
a.resizable(False,False)
a.title("燃烧别人,照亮自己-浏览器")
bing = "https://www.bing.com"
baidu = "https://www.baidu.com"
csdn = "https://www.csdn.net"
a.iconbitmap("icon.ico")
input_web = tk.Text(a, width=50, height=1)
input_web.pack()
input_web.insert('insert', 'https://www.microsoft.com')
def open_baidu():
    web.open(baidu)
def open_bing():
    web.open(bing)
def open_csdn():
    web.open(csdn)
def web_go():
    url = input_web.get('1.0', 'end')
    try:
        web.open(url)
    except:
        from tkinter import messagebox as msg
        msg.showerror('浏览错误', f'浏览器没有查询到域名对应的网站,请确认,错误域名{url}')
go_web = tk.Button(a, text="打开", command=web_go, font=("宋体", 20))
go_web.pack(fill='x')
b = tk.Label(a, text="n常用网站:", font=("楷体", 30))
b.pack()
csdn_ = Image.open("csdn.png")
csdn_load = ImageTk.PhotoImage(csdn_)
bing_ = Image.open("bing.png")
bing_load = ImageTk.PhotoImage(bing_)
baidu_ = Image.open("baidu.png")
baidu_load = ImageTk.PhotoImage(baidu_)
_baidu_ = tk.Button(a, text="百度", font=("楷体", 20), command=open_baidu)
_baidu_.pack(fill='x')
_bing_ = tk.Button(a, text="必应", font=("楷体", 20), command=open_bing)
_bing_.pack(fill='x')
_csdn_ = tk.Button(a, text="CSDN", font=("楷体", 20), command=open_csdn)
_csdn_.pack(fill='x')
a.mainloop()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号