Tkinter_Calling the system default browser through webbrowser

Preface

  Create a window application example using the tkinter library. It contains a button and a label. Clicking the button and label can call the system default browser to open the specified web page.

1. Method

import tkinter as tk
# webbrowser模块,用于调用系统默认浏览器打开网页
import webbrowser


class Open_Browser:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('演示窗口')
        self.root.geometry("300x150+1100+150")
        self.interface()

    def interface(self):
        button = tk.Button(self.root, text="打开网页", command=self.get_csdn, cursor="hand2")
        button.place(x=110, y=50)

        babel = tk.Label(self.root, text='购买高级版本', cursor="hand2")
        babel.place(x=5, y=130)
        # 标签 babel 上绑定鼠标左键点击事件的操作
        babel.bind("<Button-1>", self.open_url)

    def get_csdn(self):
        """调用系统默认浏览器,并显示网页"""
        url = "https://www.csdn.net/"  # 要加载的网址
        webbrowser.open(url)

    def open_url(self, event):
        webbrowser.open("http://www.baidu.com")


if __name__ == '__main__':
    run = Open_Browser()
    run.root.mainloop()

Guess you like

Origin blog.csdn.net/qq_45664055/article/details/131590996