繁体   English   中英

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

[英]Creating a pop-up window from custom pushbutton python

我正在尝试创建一个弹出窗口,该弹出窗口通过按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类分开,以便可以自定义它们。 我想保持这种状态,尤其是保持其清洁和可读性。

首先-请阅读: 如何创建最小,完整和可验证的示例
我做了很多工作来最小化您的代码,这浪费了大量的时间。

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

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. 在您的initUI() -init中,您调用了initUI() ,后者正在调用show()
  2. 您使用textView().show()手动调用show

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM