我的问题是当我在小部件上选择一个名字" Form1"此名称将写入文件。当我点击小部件上的OK按钮" Form1"我无法在小部件上看到相同的名称" Form2"我之前选择的。
问题在于小部件" Form2"不是最新的。我试图在小部件的开头插入self.update" Form2"但它不起作用。如何刷新小部件" 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()
答案 0 :(得分: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)