【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()

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值