📜  PyQt5 – 获取比特币价格的应用程序

📅  最后修改于: 2022-05-13 01:55:09.568000             🧑  作者: Mango

PyQt5 – 获取比特币价格的应用程序

在本文中,我们将看到如何制作一个 PyQt5 应用程序,它可以告诉比特币的实时价格。

所需模块和安装:

PyQt5: PyQt 是跨平台 GUI 工具包 Qt 的Python绑定,实现为Python插件。 PyQt 是由英国公司 Riverbank Computing 开发的免费软件。

pip install PyQt5

Requests : Requests 允许您非常轻松地发送 HTTP/1.1 请求。无需手动将查询字符串添加到您的 URL。

pip install requests

Beautiful Soup: Beautiful Soup 是一个库,可以轻松地从网页中抓取信息。它位于 HTML 或 XML 解析器之上,提供用于迭代、搜索和修改解析树的 Pythonic 习惯用法。

pip install beautifulsoup4

下面是实现——

# importing libraries
from bs4 import BeautifulSoup as BS
import requests
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("PyQt5 Bit Coin ")
  
        # setting geometry
        self.setGeometry(100, 100, 400, 600)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for widgets
    def UiComponents(self):
  
        # creating label to show bit coin price
        self.label = QLabel("//BIT-COIN//", self)
  
        # setting geometry of label
        self.label.setGeometry(50, 150, 300, 50)
  
        # setting its alignment
        self.label.setAlignment(Qt.AlignCenter)
  
        # setting font to the label
        self.label.setFont(QFont('Times', 15))
  
        # setting border to the push button
        self.label.setStyleSheet("border : 3px solid black;")
  
        # creating push button to show current price
        button = QPushButton("Show price", self)
  
        # setting geometry to push button
        button.setGeometry(150, 300, 100, 40)
  
        # adding method to the push button
        button.clicked.connect(self.show_price)
  
        # setting tool tip to button
        button.setToolTip("Press to get Bit Coin Price")        
  
          
  
    def show_price(self):
          
        # url of the bit coin price
        url = "https://www.google.com/search?q = bitcoin + price"
          
        # getting the request from url
        data = requests.get(url)
  
        # converting the text
        soup = BS(data.text, 'html.parser')
  
        # finding metha info for the current price
        ans = soup.find("div", class_="BNeawe iBp4i AP7Wnd").text
  
        # setting this price to the label
        self.label.setText(ans)
  
  
# create pyqt5 app
  
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :