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

时间:2018-02-13 14:13:12

标签: python pyqt popupwindow qpushbutton

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

#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类分开,以便我可以自定义它们。我想保持这样,特别是为了保持它的清洁和可读性。

1 个答案:

答案 0 :(得分:0)

首先请注意: How to create a Minimal, Complete, and Verifiable example 。我有很多工作要么最小化你的代码,这浪费了很多我的时间。

尽管如此,这里有一个最小的工作代码,有你自己的按钮类:

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()创建它:

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. 手动呼叫节目

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

相关问题