V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
daaa2019
V2EX  ›  Python

pycharm+pyqt5,点击按钮更改 Label 的文字,不能直接显示,需要把界面刷新下才能更新 Lable 的文字?

  •  
  •   daaa2019 · 2020-03-02 00:49:29 +08:00 · 5629 次点击
    这是一个创建于 1509 天前的主题,其中的信息可能已经有所发展或是发生改变。
    实际上文本应该已经改掉了,但是在屏幕上看到的还是老的文字,需要最小化再打开,或者和其他程序界面切换一下,比如 Ctrl+Tab 这样弄下,才刷新出来……这是什么情况呢?
    我是 mbp ( 10.15.3 )+Pycharm ( 2019.3 )+Pyqt5
    请大家说下这是什么诡异的情况……
    15 条回复    2020-10-27 18:07:30 +08:00
    daaa2019
        1
    daaa2019  
    OP
       2020-03-02 00:52:04 +08:00
    chengxiao
        2
    chengxiao  
       2020-03-02 02:38:24 +08:00
    你是不是在主线程里更新 UI 了
    Yjx97
        3
    Yjx97  
       2020-03-02 08:24:45 +08:00
    QtDesinger 做这个可以
    David1119
        4
    David1119  
       2020-03-02 09:17:54 +08:00
    试了,没问题。那个事件绑定放在 setupUi 里面,别放 init 里
    daaa2019
        5
    daaa2019  
    OP
       2020-03-02 12:47:48 +08:00
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
    from PyQt5.QtCore import pyqtSignal, Qt
    from testLabelText import Ui_Form


    class MyMainForm(QMainWindow, Ui_Form):

    def __init__(self):
    super(MyMainForm, self).__init__()
    self.setupUi(self)
    self.pushButton.clicked.connect(self.printLabel2)
    # self.initUi()

    # def initUi(self):


    def printLabel2(self):
    print("改变 Label 的文本")
    self.label.setText("新的文字")
    print("当前 Label 的文本是", self.label.text())


    if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyMainForm()
    win.show()
    sys.exit(app.exec_())
    daaa2019
        6
    daaa2019  
    OP
       2020-03-02 12:48:05 +08:00
    @chengxiao 我回帖贴了代码,你帮我看看
    daaa2019
        7
    daaa2019  
    OP
       2020-03-02 12:48:31 +08:00
    @Yjx97 是的,ui 用 designer 做的
    daaa2019
        8
    daaa2019  
    OP
       2020-03-02 12:53:23 +08:00
    @David1119 昨晚我是放 init 里的
    刚刚看你这么说我就把事件在 designer 里绑定,放在 setupUI 了,然后把 init 里的绑定注释掉,也不行。
    到底我这个说代码问题呢,还是说系统问题。。。

    附上 setupUI 的代码


    from PyQt5 import QtCore, QtGui, QtWidgets


    class Ui_Form(object):
    def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(547, 318)
    self.label = QtWidgets.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(240, 210, 58, 16))
    self.label.setObjectName("label")
    self.pushButton = QtWidgets.QPushButton(Form)
    self.pushButton.setGeometry(QtCore.QRect(70, 200, 112, 32))
    self.pushButton.setObjectName("pushButton")

    self.retranslateUi(Form)
    self.pushButton.clicked.connect(Form.printLabel2)
    QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.label.setText(_translate("Form", "Label"))
    self.pushButton.setText(_translate("Form", "PushButton"))
    daaa2019
        9
    daaa2019  
    OP
       2020-03-02 13:06:55 +08:00
    会不会是 pycharm 或者系统的问题?
    我复制了 https://github.com/maicss/PyQt5-Chinese-tutorial/blob/master/%E6%8E%A7%E4%BB%B61.md
    切换按钮的一段代码进去,也是会出现类似问题……
    1462326016
        10
    1462326016  
       2020-03-02 16:12:32 +08:00
    实测,运行你贴在上边的代码,没有问题,实时更改了文字。命令行运行的
    daaa2019
        11
    daaa2019  
    OP
       2020-03-02 17:15:04 +08:00
    @1462326016 妈呀,我这是怎么回事呢
    daaa2019
        12
    daaa2019  
    OP
       2020-03-02 17:21:36 +08:00
    看来代码应该没问题,那我这种情况,有没有哪位同学告诉我说什么问题呢,系统?还是 pycharm ?还是 pyqt5 的问题?
    1462326016
        13
    1462326016  
       2020-03-02 19:27:25 +08:00
    @daaa2019 不清楚,忘了说一件事,我是 windows。还有,应该不用怀疑 pycharm,因为 pycharm 就是用命令行帮你运行脚本而已(不调试的话)
    xiaodouya
        14
    xiaodouya  
       2020-09-18 14:51:42 +08:00
    def printLabel2(self):
    print("改变 Label 的文本")
    self.label.setText("新的文字")
    #加一行
    self.label.repaint()
    print("当前 Label 的文本是", self.label.text())
    yunhu
        15
    yunhu  
       2020-10-27 18:07:30 +08:00
    主线程能直接更新 ui 吗 不行吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5239 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 08:47 · PVG 16:47 · LAX 01:47 · JFK 04:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.