简体   繁体   English

PyQt小部件重新加载或刷新

[英]PyQt widget reload or refresh

My problem is that when I choose a name on widget "Form1" this name is written to file. 我的问题是,当我在小部件“ Form1”上选择一个名称时,该名称将写入文件。 When I click on OK button on widget "Form1" I can't see the same name on widget "Form2" which I chose previously. 当我单击小部件“ Form1”上的“确定”按钮时,在小部件“ Form2”上看不到以前选择的相同名称。

The problem is that the widget "Form2" is not up to date. 问题是窗口小部件“ Form2”不是最新的。 I tried to insert self.update at the beginning on the widget "Form2" but it doesn't work. 我试图在窗口小部件“ Form2”的开头插入self.update,但是它不起作用。 How can I refresh widget "Form2" or reload the content of the file? 如何刷新小部件“ Form2”或重新加载文件内容?

I have a simple code: 我有一个简单的代码:

#!/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()

There are a couple ways you could do this. 您可以通过几种方法来执行此操作。 The first thing you need is a "refresh" function on form2 that will update the text on form2. 您需要的第一件事是在form2上的“刷新”功能,该功能将更新form2上的文本。

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

The second thing you need is a way to call that refresh function whenever the file is rewritten. 您需要做的第二件事是每当文件被重写时调用refresh函数的方法。

You could just use the existing signal that form1 already emits to show form2. 您可以只使用form1已经发出的现有信号来显示form2。 In your MainWidget, connect that signal to the refresh function 在您的MainWidget中,将该信号连接到刷新功能

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

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

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