【问题标题】:How to resize font size in PyQt5 GUI?如何在 PyQt5 GUI 中调整字体大小?
【发布时间】:2021-05-03 16:36:54
【问题描述】:

我很快就完成了我的 WhatsApp 机器人。但是,这些天我发现了一个问题,试图在我的普通笔记本电脑上使用它而不是在我的显示器上使用它。屏幕大小发生变化,PyQt5 GUI 也随之变化。最大的问题是我设置了字体大小,并且除了按钮 usw 之外,它不会通过调整大小来改变。

这是我的代码:

from functools import cached_property
import sys
import threading
import time

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Worker:
    def start(self, amount, adressed, message):
        threading.Thread(target=self._execute, args=(amount, adressed, message), daemon=True).start()

    def _execute(self, amount, adressed, message):
        profile_path = "user-data-dir=/home/daniel/.config/google-chrome/storing1"

        options = webdriver.ChromeOptions()
        options.add_argument(profile_path)
        browser = webdriver.Chrome(executable_path="./chromedriver", options=options)
        browser.get("https://web.whatsapp.com")

        time.sleep(5)
        search = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located(
            (By.CSS_SELECTOR, "#side > div.SgIJV > div > label > div > div._2_1wd.copyable-text.selectable-text")))
        search.send_keys(adressed)
        time.sleep(1)
        search.send_keys(Keys.ENTER)
        send = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,
                                                                                                  "#main > footer > div.vR1LG._3wXwX.copyable-area > div._2A8P4._2A1WX > div > div._2_1wd.copyable-text.selectable-text")))

        for i in range(amount):
            send.send_keys(message)
            send.send_keys(Keys.ENTER)



class interface(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initMe()

    def initMe(self):
        self.boldFont = QFont()
        self.boldFont.setBold(True)
        self.setGeometry(1200, 250, 600, 500)
        self.title = self.setWindowTitle("Whatsapp Bot interface")
        self.Layout_components()
        self.labels()
        self.input()
        self.button()
        self.setFont(QFont("Arial", 15))
        self.setWindowIcon(QIcon("Bot.png"))

        self.show()

    def Layout_components(self):
        self.exit_ = QAction(QIcon("Bot.png"), "Exit", self)
        self.stop_ = QAction(QIcon("Bot.png"), "Stop", self)
        self.start_ = QAction(QIcon("Bot.png"), "Start", self)
        self.exit_.triggered.connect(self.exit)
        self.stop_.triggered.connect(self.stop)
        self.start_.triggered.connect(self.start)

        menubar = self.menuBar()
        file = menubar.addMenu("Bot")
        file.addAction(self.exit_)
        file.addAction(self.stop_)
        file.addAction(self.start_)

    def labels(self):
        labelp1 = QLabel("picture", self)
        labelp1.setPixmap(QPixmap("Bot.png"))
        labelp1.setGeometry(340, 100, 210, 180)

        labelp2 = QLabel("picture1", self)
        labelp2.setPixmap(QPixmap("whatsapp_symbol.png"))
        labelp2.setScaledContents(True)
        labelp2.resize(80, 80)
        labelp2.setGeometry(50, 400, 80, 80)

        label1 = QLabel("description", self)
        label1.setGeometry(50, 40, 500, 50)
        label1.setText(
            "Welcome to my Whatsappbot. Are you ready to annoy other people with endless spam up. Have fun!!!")
        label1.setFont(QFont("Arial", 14))
        label1.setStyleSheet("border : 2px solid black;")
        label1.setWordWrap(True)

        label2 = QLabel("adress", self)
        label2.setGeometry(50, 100, 150, 50)
        label2.setText("adressed to: ")

        label2 = QLabel("message", self)
        label2.setGeometry(50, 300, 150, 50)
        label2.setText("message: ")

        label2 = QLabel("amount", self)
        label2.setGeometry(50, 200, 200, 50)
        label2.setText("amount of messages: ")

        self.label_input = QLabel("counter", self)
        self.label_input.setGeometry(170, 260, 80, 20)
        self.label_input.setFont(QFont("Arial", 15))
        self.label_input.setAlignment(Qt.AlignCenter)
        self.label_input.setStyleSheet("border:2px solid black")

    def input(self):
        self.input_adressed = QLineEdit(self)
        self.input_adressed.setGeometry(50, 150, 150, 30)

        self.input_message = QLineEdit(self)
        self.input_message.setGeometry(50, 350, 150, 30)

        self.input_amount = QSlider(self)
        self.input_amount.setGeometry(50, 240, 100, 70)
        self.input_amount.setMaximum(1000)

        self.input_amount.valueChanged.connect(self.label_input.setNum)

    def button(self):
        self.but_stop = QPushButton("Stop", self)
        self.but_stop.setGeometry(340, 360, 210, 50)
        self.but_stop.setFont(self.boldFont)
        self.but_stop.clicked.connect(self.stop)

        self.but_start = QPushButton("Start", self)
        self.but_start.setGeometry(340, 300, 210, 50)
        self.but_start.setFont(self.boldFont)
        self.but_start.clicked.connect(self.start)

        self.but_exit = QPushButton("Exit", self)
        self.but_exit.setGeometry(340, 420, 210, 50)
        self.but_exit.setFont(self.boldFont)
        self.but_exit.clicked.connect(self.exit)

    @cached_property
    def worker(self):
        return Worker()

    @property
    def amount(self):
        value = self.input_amount.value()
        return value

    @property
    def adressed(self):
        return self.input_adressed.text()

    @property
    def message(self):
        return self.input_message.text()

    def stop(self):
        print("programm stopps ...")
        QCoreApplication.quit()


    def exit(self):
        QCoreApplication.quit()

    def start(self):
        pop_up = QMessageBox(self)
        pop_up.setWindowTitle("Start Bot")
        pop_up.setText("Do you really wanna start the spam ?!")
        pop_up.setIcon(QMessageBox.Question)
        pop_up.setStandardButtons(QMessageBox.Cancel | QMessageBox.No | QMessageBox.Yes)
        pop_up.setDefaultButton(QMessageBox.Yes)
        if pop_up.exec_() == QMessageBox.Yes:
            print("programm starts ...")
            self.worker.start(self.amount, self.adressed, self.message)


app = QApplication(sys.argv)
x = interface()
sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:
    1. 使用setFont():它为目标设置默认字体;您可以使用 something.font() 获取当前默认字体,然后使用 font.setPointSize()(或 setPointSizeF() 获取浮点值,如果字体允许),然后在目标上调用 setFont(font)。
    2. 在目标setStyleSheet()中使用字体[-*];

    【讨论】:

      猜你喜欢
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多