从自定义按钮 python 创建弹出窗口 window

Creating a pop-up window from custom pushbutton python

我正在尝试创建一个弹出窗口 window,它通过按下 QPushButton 弹出。但是,我有一个单独的 QPushButton class 我想使用。我似乎无法让它工作。我做错了什么吗?

#import ... statements
import sys

# from ... import ... statements
from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QGridLayout, QWidget, QHBoxLayout, QLabel,
                             QVBoxLayout)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QFontDatabase, QColor, QPalette, QMovie
from skimage import transform, io

# Create main window of the widget
class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        #Set a title inside the widget
        self.titleLabel = QLabel()
        titleText = "some title text"
        self.titleLabel.setText(titleText)

        # Give the label some flair
        self.titleLabel.setFixedWidth(1000)
        self.titleLabel.setAlignment(Qt.AlignCenter)

        QFontDatabase.addApplicationFont(link_to_custom_font)
        font = QFont()
        font.setFamily("custom_font_name")
        font.setPixelSize(50)
        self.titleLabel.setFont(font)


        # Set first button - The "Yes" Button
        self.btn1 = myButtonOne("Yes")

        #Initialize GUI
        self.layoutGUI()
        self.initUI()

    def initUI(self):
        self.fromleft = 200
        self.fromtop = 100
        self.w = 1000
        self.h = 500

        self.setGeometry(self.fromleft, self.fromtop, self.w, self.h)

    def layoutGUI(self):
        hbox = QHBoxLayout()
        hbox.setSpacing(20)

        hbox.addWidget(self.btn1)

        vbox = QVBoxLayout()
        vbox.addWidget(self.titleLabel)
        vbox.addLayout(hbox)

        self.setLayout(vbox)

class myButtonOne(QPushButton):
    def __init__(self, parent=None):
        super(myButtonOne, self).__init__(parent)

        # Set maximum border size
        imSize = io.imread(imagePath)
        imHeight = imSize.shape[1]
        imWidth = imSize.shape[0]

        # Set first button - The "Yes" Button
        yesImage = someImagePath
        self.setStyleSheet("background-image: url(" + yesImage + ");"
                           "background-repeat: none;"
                           "background-position: center;"
                                                                 "border: none")
        self.setFixedSize(imWidth, imHeight)

        self.clicked.connect(self.buttonOnePushed)

    def buttonOnePushed(self):
        textView().show()

    def enterEvent(self, event):
        newImage = someOtherImagePath
        self.setStyleSheet("background-image: url("+newImage+");"
                           "background-repeat: none;"
                           "background-position: center;"
                                                                 "border: none")

    def leaveEvent(self, event):
        newImage = someImagePath
        self.setStyleSheet("background-image: url("+newImage+");"
                           "background-repeat: none;"
                           "background-position: center;"
                                                                 "border: none")

class textView(QWidget):
    def __init(self):
        textView.__init__()

        theText = QLabel()
        #define sizes
        self.height = 550
        self.width = 250

        # Open QWidget
        self.initUI()

        # Set the text for the QLabel
        someText = "Some Text for the label"
        theText.setText(someText)


    def initUI(self):
        self.show()

# Start GUI
if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

因此,我试图将 QPushButton class 分开,以便我可以自定义它们。我想保持这样,特别是保持它的清洁和可读性。

首先 - 请阅读:How to create a Minimal, Complete, and Verifiable example
我做了很多工作来最小化你的代码,这浪费了很多 我的 时间。

尽管如此,这里是 一个最小的工作代码,带有您自己的按钮 class:

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget,  QLabel, QVBoxLayout

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.titleLabel = QLabel( "some label text" )
        self.btn1 = myButtonOne( "button text" )

        hbox = QVBoxLayout() # one vertical box seemed enough
        hbox.addWidget( self.titleLabel )
        hbox.addWidget( self.btn1 )
        self.setLayout( hbox )

class myButtonOne(QPushButton):
    def __init__(self, text, parent=None):
        super(myButtonOne, self).__init__(text, parent)
        self.clicked.connect(self.buttonOnePushed)
        # add your customizations here

    def buttonOnePushed (self) :
        self.t = textView()
        self.t.show()

class textView(QWidget):
    def __init__(self):
        super(textView, self).__init__()
        self.theText = QLabel('test', self )

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())

你的代码哪里做错了?

使用 textView().show() 创建一个本地版本的 textView-class 和 show()'s it:

def buttonOnePushed(self):
    textView().show()

但是,show() 意味着代码继续,代码结束的地方,这会导致清理局部变量。结束 - 它只显示一微秒。

def buttonOnePushed (self) :
    self.t = textView()
    self.t.show()

上面的代码将 var 存储为按钮的实例属性,未清理。


此外,您在 textView-class 中拼错了 init: "__init" 应该是 __init__ - 否则在使用构造函数时不会调用它:

class textView(QWidget):
    def __init(self):
        textView.__init__()

最后,您想调用 show() 两次:

  1. 在您的 textView-init 中,您调用 initUI() 调用 show()
  2. 您使用 textView().show()
  3. 手动调用节目

希望这对您有所帮助!为了便于阅读,我没有包括您的个人风格调整。