PyQt 小部件重新加载或刷新
PyQt widget reload or refresh
我的问题是,当我在小部件上选择一个名称时 "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()
有几种方法可以做到这一点。您首先需要的是 form2 上的 "refresh" 函数,它将更新 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)
我的问题是,当我在小部件上选择一个名称时 "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()
有几种方法可以做到这一点。您首先需要的是 form2 上的 "refresh" 函数,它将更新 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)