为什么我的小部件不更新,除非我切换到另一个窗口?

2024-07-04 05:50:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我刚开始用PyQt5。现在我只编写了一个按钮,它应该从行编辑中获取文本,将其存储在一个全局变量中,并将其放入文本浏览器中。现在它做到了。。。但是有问题。 在我单击其他程序/窗口,然后再次单击我的应用程序之前,文本浏览器不会更新 当行编辑被清除时,有一个错误,基本上文本没有被正确清除,但只是它的上半部分。当我再次输入时,这个就消失了。

我尝试为小部件调用.update()方法QApplication.process_事件()

这是密码

from PyQt5 import QtCore, QtGui, QtWidgets

lyrics = ''
adlib = ' (Placeholder adlib)'

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(742, 680)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.addLineBtn = QtWidgets.QPushButton(self.centralwidget)
        self.addLineBtn.setGeometry(QtCore.QRect(530, 0, 91, 51))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setItalic(True)
        self.addLineBtn.setFont(font)
        self.addLineBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.addLineBtn.setObjectName("addLineBtn")
        self.deleteBtn = QtWidgets.QPushButton(self.centralwidget)
        self.deleteBtn.setGeometry(QtCore.QRect(120, 80, 91, 32))
        self.deleteBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.deleteBtn.setObjectName("deleteBtn")
        self.saveBtn = QtWidgets.QPushButton(self.centralwidget)
        self.saveBtn.setGeometry(QtCore.QRect(30, 80, 91, 32))
        self.saveBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.saveBtn.setObjectName("saveBtn")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(30, 20, 501, 51))
        self.lineEdit.setObjectName("lineEdit")
        self.dialLabel = QtWidgets.QLabel(self.centralwidget)
        self.dialLabel.setGeometry(QtCore.QRect(640, 20, 71, 16))
        self.dialLabel.setObjectName("dialLabel")
        self.rtdSlider = QtWidgets.QSlider(self.centralwidget)
        self.rtdSlider.setGeometry(QtCore.QRect(620, 40, 101, 22))
        self.rtdSlider.setOrientation(QtCore.Qt.Horizontal)
        self.rtdSlider.setObjectName("rtdSlider")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(20, 120, 701, 501))
        self.textBrowser.setObjectName("textBrowser")
        self.noadlibBtn = QtWidgets.QPushButton(self.centralwidget)
        self.noadlibBtn.setGeometry(QtCore.QRect(530, 50, 91, 51))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setItalic(True)
        self.noadlibBtn.setFont(font)
        self.noadlibBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.noadlibBtn.setObjectName("noadlibBtn")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 742, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.addLineBtn.clicked.connect(self.addLineAdlib)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.addLineBtn.setText(_translate("MainWindow", "Adlib"))
        self.deleteBtn.setText(_translate("MainWindow", "Delete"))
        self.saveBtn.setText(_translate("MainWindow", "Save"))
        self.dialLabel.setText(_translate("MainWindow", "RTD Level"))
        self.noadlibBtn.setText(_translate("MainWindow", "No Adlib"))

    def addLineAdlib(self):
        global lyrics
        lyrics += self.lineEdit.text() + adlib + '\n'
        self.lineEdit.clear()
        self.textBrowser.setText(lyrics)
    def addLineNoAdlib(self):
        pass

    def save(self):
        pass

    def deleteLine(self):
        pass


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Tags: selfdeftranslatefontqtguiqtcoreqtwidgetssetobjectname
2条回答

这是一个更干净的、更具类化(pythonicpyqt)版本的程序,我把注释掉的东西放在里面,让你看看它应该在哪里,但是我删除了你的坐标系,用创建GUI的更标准的布局样式替换了它。在

我在win10上用python3.7pyqt5测试了这个问题,所以如果有操作系统的问题,你会知道的,但是我的猜测是,在某些地方出现了一个断开连接的问题,而这正是您遇到的问题。在

同样,你的按钮设计永远不会创建一个空白行,因为它总是把一些东西放在一行,我只需点击按钮而不输入任何东西

from sys import exit as sysExit

from PyQt5.QtCore import Qt
from PyQt5.QtGui  import QFont, QCursor
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QDockWidget, QStyleFactory
from PyQt5.QtWidgets import QPushButton, QLineEdit, QLabel, QSlider, QTextBrowser, QMenuBar, QStatusBar

class MenuToolBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
        self.MainWin = MainWin
        self.MainMenu = MainWin.menuBar()

        # ******* Create the Help Menu *******
        self.HelpMenu  = self.MainMenu.addMenu('Help')

class CenterPanel(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)

    # General Font Object for a couple of Buttons
        btnFont = QFont()
        btnFont.setFamily('Arial')
        btnFont.setItalic(True)

    # First Item Horizontal Box 1 Containing the AddLib Entry and Button
        self.lnAdlibEntry = QLineEdit(self)
#        self.lnAdlibEntry.setGeometry(QRect(30, 20, 501, 51))
#        self.lnAdlibEntry.resize(501, 51)

        self.btnAddLine = QPushButton(self)
#                                       Left, Top, Width, Height
#        self.btnAddLine.setGeometry(QRect(530, 0, 91, 51))
#                            Width, Height
#        self.btnAddLine.resize(91, 51)
        self.btnAddLine.setFont(btnFont)
        self.btnAddLine.setCursor(QCursor(Qt.PointingHandCursor))
        self.btnAddLine.setText('Adlib')
        self.btnAddLine.clicked.connect(parent.AddLineAdlib)

        HBox1 = QHBoxLayout()
        HBox1.addWidget(self.lnAdlibEntry)
        HBox1.addWidget(self.btnAddLine)

    # Second Item Vertical Box 1 Containing the AdlibEntry LineEdit and RTD Label and RTD Slider

        self.lblDial = QLabel(self)
#        self.lblDial.setGeometry(QRect(640, 20, 71, 16))
#        self.lblDial.resize(71, 16)
        self.lblDial.setText("RTD Level")

        self.sldrRtd = QSlider(self)
#        self.sldrRtd.setGeometry(QRect(620, 40, 101, 22))
#        self.sldrRtd.resize(101, 22)
        self.sldrRtd.setOrientation(Qt.Horizontal)

        VBox1 = QVBoxLayout()
        VBox1.addWidget(self.lblDial)
        VBox1.addWidget(self.sldrRtd)

    # Third Item Horizontal Box 2 containing the Save, No Adlib and Delete buttons
        self.btnNoAdlib = QPushButton(self)
#        self.btnNoAdlib.setGeometry(QRect(530, 50, 91, 51))
#        self.btnNoAdlib.resize(91, 51)
        self.btnNoAdlib.setFont(btnFont)
        self.btnNoAdlib.setCursor(QCursor(Qt.PointingHandCursor))
        self.btnNoAdlib.setText("No Adlib")

        self.btnSave = QPushButton(self)
#        self.btnSave.setGeometry(QRect(30, 80, 91, 32))
#        self.btnSave.resize(91, 32)
        self.btnSave.setCursor(QCursor(Qt.PointingHandCursor))
        self.btnSave.setText('Save')

        self.btnDelete = QPushButton(self)
#        self.btnDelete.setGeometry(QRect(120, 80, 91, 32))
#        self.btnDelete.resize(91, 32)
        self.btnDelete.setCursor(QCursor(Qt.PointingHandCursor))
        self.btnDelete.setText('Delete')

        HBox2 = QHBoxLayout()
        HBox2.addWidget(self.btnSave)
        HBox2.addStretch(1)
        HBox2.addWidget(self.btnNoAdlib)
        HBox2.addStretch(1)
        HBox2.addWidget(self.btnDelete)

    # Sixth Item Text Browser
        self.txtBrowser = QTextBrowser(self)
#        self.txtBrowser.setGeometry(QRect(20, 120, 701, 501))
#        self.txtBrowser.resize(701, 501)

        VBox2 = QVBoxLayout()
        VBox2.addLayout(HBox1)
        VBox2.addLayout(VBox1)
        VBox2.addLayout(HBox2)
        VBox2.addWidget(self.txtBrowser)

        self.setLayout(VBox2)

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

        self.Lyrics = ''
        self.Adlib = ' (Placeholder adlib)'

        self.setWindowTitle('Main Window')
        self.resize(742, 680)

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.MenuBar = MenuToolBar(self)
#        self.MenuBar.setGeometry(QRect(0, 0, 742, 22))

        self.SetStatusBar(self)
        self.setStyle(QStyleFactory.create('Cleanlooks'))

    def SetStatusBar(self, parent):
        StatusMsg = ''
        parent.StatBar = parent.statusBar()

        if len(StatusMsg) < 1:
            StatusMsg = 'Ready'

        parent.StatBar.showMessage(StatusMsg)

    def AddLineAdlib(self):
      # This statement retains everything previously in Lyrics and 
      # everything in the AdlibEntry box and everything in Adlib which
      # I am not sure is what you are wanting but it is what you had
        self.Lyrics += self.CenterPane.lnAdlibEntry.text() + self.Adlib + '\n'
        self.CenterPane.lnAdlibEntry.clear()
        self.CenterPane.txtBrowser.setText(self.Lyrics)

    def AddLineNoAdlib(self):
        pass

    def Save(self):
        pass

    def DeleteLine(self):
        pass

if __name__ == "__main__":
    MainThred = QApplication([])

    MainGUI = MainWindow()
    MainGUI.show()

    sysExit(MainThred.exec_())

编辑:我有一个奇怪的箭头出现在我的屏幕上,它似乎QStyleFactory调用删除了,我调整了StatusBar声明,使其更模块化,以防以后您想类化它


1The text browser does NOT update until I click another program/window and then click my app again-此问题可能与您的操作系统有关。我在Ubuntu16.04上有错误的PyQT5行为,而在ubuntu18.04上一切正常。你能分享更多信息吗?
注意:我在Ubuntu18.04上测试了你的脚本,我不需要点击其他程序/窗口来刷新它

2When the line edit is cleared there is a bug which is basically text not being cleared properly but only it's top half. This goes away when I type again.-你能解释一下你说的it's top half是什么意思吗?如果您的意思是仍然存在(Placeholder adlib)文本,那么在向歌词变量添加内容之前,您应该修改函数以选中lineEdit文本框。例如,像这样:

def addLineAdlib(self):
        if self.lineEdit.text():
                global lyrics
                lyrics += self.lineEdit.text() + adlib + '\n'
                self.lineEdit.clear()
                self.textBrowser.setText(lyrics)

相关问题 更多 >

    热门问题