【问题标题】:PyQt widget reload or refreshPyQt 小部件重新加载或刷新
【发布时间】:2016-05-28 03:48:42
【问题描述】:

我的问题是,当我在小部件“Form1”上选择一个名称时,此名称会写入文件。当我单击小部件“Form1”上的“确定”按钮时,我在之前选择的小部件“Form2”上看不到相同的名称。

问题是小部件“Form2”不是最新的。我试图在小部件“Form2”的开头插入 self.update,但它不起作用。如何刷新小部件“Form2”或重新加载文件的内容?

我有一个简单的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys

from functools import partial
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import QtGui, QtCore
from math import sqrt
from time import gmtime, strftime

class Form1(QWidget):
    showForm2Signal = pyqtSignal()

    def __init__(self, parent=None):
        super(Form1, self).__init__(parent)

        self.label = QtGui.QLabel(self)
        self.label.setGeometry(0, 40, 480, 400)
        self.label.move(0,40)

        #Select name
        self.styleTerminalSettings = QtGui.QLabel("Please select your name:", self)
        self.styleTerminalSettings.move(20,40)
        self.styleTerminalSettings.resize(250,30)
        self.styleTerminalSettings.setStyleSheet("color: black;  background-color: transparent; font-size: 12pt; font-weight: bold;")

        self.comboBox = QtGui.QComboBox(self)
        self.comboBox.addItem("NAME 1")
        self.comboBox.addItem("NAME 2")
        self.comboBox.addItem("NAME 3")
        self.comboBox.addItem("NAME 4")
        self.comboBox.move(20,80)
        self.comboBox.resize(440,50)
        self.comboBox.currentIndexChanged.connect(self.selectionchange)

        #OK button
        self.ok_button = QtGui.QPushButton("OK", self)
        self.ok_button.resize(self.ok_button.minimumSizeHint())
        self.ok_button.move(0,340)
        self.ok_button.resize(480,60)
        self.ok_button.setStyleSheet("color: #25373D; background-color: #71BA51;  font-size: 16pt; font-weight: bold;")

        layout = QVBoxLayout(self)

        self.ok_button.clicked.connect(self.showForm2Signal.emit)

    def selectionchange(self,i): 
        pos_user_name = self.comboBox.currentText()
        self.users_write(pos_user_name)

    #Name write to file 
    def users_write(self, pos_user_name):
        filename = "user_name_session"

        target = open(filename, 'w')
        target.truncate()
        target.write(pos_user_name)
        target.close()    

class Form2(QWidget):
    def __init__(self, parent=None):
        super(Form2, self).__init__(parent)

        global pos_user_name 

        self.label = QtGui.QLabel(self)
        self.label.setGeometry(0, 40, 480, 400)
        self.label.move(0,40)

        #Name read from file
        filename = "user_name_session"
        target = open(filename, "r+")
        name = target.read(10);

        self.styleCashRegister = QtGui.QLabel("Name:", self)
        self.styleCashRegister.move(20,40)
        self.styleCashRegister.resize(170,30)
        self.styleCashRegister.setStyleSheet("color: black; background-color: transparent;  font-size: 16pt; font-weight: bold;")

        self.cashregisterid = QtGui.QLineEdit(self)
        self.cashregisterid.setText(str(name))
        self.cashregisterid.move(100, 40)
        self.cashregisterid.resize(260,30)
        self.cashregisterid.setStyleSheet("color: #25373D;  font-size: 16pt; font-weight: bold;")

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.stack = QStackedWidget()
        layout = QVBoxLayout(self)
        layout.addWidget(self.stack)
        layout.setContentsMargins(0, 0, 0, 0)

        self.setGeometry(0, 0, 480, 400)
        self.setWindowTitle("PYQT WIDGET TEST")
        self.setStyleSheet("background-color: #E8E8E8")

        self.form1 = Form1(self)
        self.form2 = Form2(self)

        self.stack.addWidget(self.form1)
        self.stack.addWidget(self.form2)

        self.form1.showForm2Signal.connect(partial(self.stack.setCurrentWidget,self.form2))

        self.stack.setCurrentWidget(self.form1) 

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    w.show()
    app.exec_()
    sys.exit()

【问题讨论】:

    标签: python qt widget pyqt reload


    【解决方案1】:

    有几种方法可以做到这一点。您需要的第一件事是 form2 上的“刷新”功能,它将更新 form2 上的文本。

    def refresh(self):
        filename = "user_name_session"
        target = open(filename, "r+")
        name = target.read(10);
        self.cashregisterid.setText(str(name))
    

    您需要的第二件事是在文件被重写时调用refresh 函数。

    您可以只使用 form1 已经发出的现有信号来显示 form2。在您的 MainWidget 中,将该信号连接到刷新功能

    self.form1.showForm2Signal.connect(self.form2.refresh)
    

    【讨论】:

    • Brendad Abel:我试过你的建议,但没有用。也许这个信号不好?请改写我的代码。
    • 什么不起作用?你收到错误了吗? refresh 函数没有被调用吗?
    • 我没有收到错误。我测试了刷新功能...我插入了 print("ok") ,我可以在终端上看到它。但我看不到小部件“Form2”,只有“Form1”连续。
    • 您之前没有看到过form2,它只是没有更新吗?您是否删除了其他信号连接?您应该有 2 个信号连接——一个用于切换堆栈小部件,一个用于调用刷新函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多