簡體   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