如何刷新Qwidget以显示当前信息?
我有一个Pyside2 GUI,该GUI接受了第一页的用户的数字,然后进行一些计算,并在第二页上显示结果。每个页面都是qstackedwidget中的Qwidget。第二页的“结果”页面上有一个按钮,该页面将用户发送回第一页以输入新号码。
我的问题是,当我输入新数时,结果永远不会从第一个数字变化。我使用打印语句确认结果页面上的标签正在更新,但显示器保持不变。
# importing the module
import os
import sys
from PySide2 import QtWidgets
import PySide2.QtUiTools as QtUiTools
class IncomeScreen(QtWidgets.QMainWindow):
def __init__(self):
super(IncomeScreen, self).__init__()
# Load the IncomeScreen ui
loader = QtUiTools.QUiLoader()
path = os.path.join(os.path.dirname(__file__), "main.ui")
self.main = loader.load(path, self)
# Connect the signals with custom slots
self.main.calculate_pushButton.clicked.connect(self.calculate)
def calculate(self):
init_amount = self.main.income_lineEdit.text()
IncomeScreen.init_amount = float(init_amount)
# Create an instance of DistributionScreen class
self.distribution = DistributionScreen()
# Add DistributionScreen to the stacked widget
widget.addWidget(self.distribution)
# Change index to show DownloadPage
widget.setCurrentIndex(widget.currentIndex()+1)
class DistributionScreen(QtWidgets.QMainWindow):
def __init__(self):
super(DistributionScreen, self).__init__()
loader = QtUiTools.QUiLoader()
path = os.path.join(os.path.dirname(__file__), "dialog.ui")
self.dialog = loader.load(path, self)
# Set initial amount to label
self.dialog.initialAmount_label.setText(str(IncomeScreen.init_amount))
print("Initial Amount = {:0.2f}".format(IncomeScreen.init_amount))
# 10 Percent
ten = IncomeScreen.init_amount * 0.1
print("10% = {:0.2f}".format(ten))
self.dialog.label_10percent.setText("{:0.2f}".format(ten))
print(self.dialog.label_10percent.text())
# 20 percent
twenty = IncomeScreen.init_amount * 0.2
print("20% = {:0.2f}".format(twenty))
self.dialog.label_20percent.setText("{:0.2f}".format(twenty))
print(self.dialog.label_20percent.text())
# Update widget
self.dialog.update()
# Connect the signals with custom slots
self.dialog.reset_pushButton.clicked.connect(self.reset)
def reset(self):
print("reset")
# Change index to show IncomeScreen
widget.setCurrentIndex(widget.currentIndex()-1)
# main
# if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
income = IncomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(income)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
另外,我正在使用Python 3.7.4
编辑:您可以下载UI文件
I have a PySide2 GUI that accepts a number from the user on page one then does some calculations and displays the results on page two. Each page is a QWidget within a QStackedWidget. There is a pushbutton on page two, the results page, that sends the user back to page one to enter a new number.
My problem is that when I enter a new number the results never change from the first number. I use print statements to confirm that the labels on the results page are updating but the display stays the same.
# importing the module
import os
import sys
from PySide2 import QtWidgets
import PySide2.QtUiTools as QtUiTools
class IncomeScreen(QtWidgets.QMainWindow):
def __init__(self):
super(IncomeScreen, self).__init__()
# Load the IncomeScreen ui
loader = QtUiTools.QUiLoader()
path = os.path.join(os.path.dirname(__file__), "main.ui")
self.main = loader.load(path, self)
# Connect the signals with custom slots
self.main.calculate_pushButton.clicked.connect(self.calculate)
def calculate(self):
init_amount = self.main.income_lineEdit.text()
IncomeScreen.init_amount = float(init_amount)
# Create an instance of DistributionScreen class
self.distribution = DistributionScreen()
# Add DistributionScreen to the stacked widget
widget.addWidget(self.distribution)
# Change index to show DownloadPage
widget.setCurrentIndex(widget.currentIndex()+1)
class DistributionScreen(QtWidgets.QMainWindow):
def __init__(self):
super(DistributionScreen, self).__init__()
loader = QtUiTools.QUiLoader()
path = os.path.join(os.path.dirname(__file__), "dialog.ui")
self.dialog = loader.load(path, self)
# Set initial amount to label
self.dialog.initialAmount_label.setText(str(IncomeScreen.init_amount))
print("Initial Amount = {:0.2f}".format(IncomeScreen.init_amount))
# 10 Percent
ten = IncomeScreen.init_amount * 0.1
print("10% = {:0.2f}".format(ten))
self.dialog.label_10percent.setText("{:0.2f}".format(ten))
print(self.dialog.label_10percent.text())
# 20 percent
twenty = IncomeScreen.init_amount * 0.2
print("20% = {:0.2f}".format(twenty))
self.dialog.label_20percent.setText("{:0.2f}".format(twenty))
print(self.dialog.label_20percent.text())
# Update widget
self.dialog.update()
# Connect the signals with custom slots
self.dialog.reset_pushButton.clicked.connect(self.reset)
def reset(self):
print("reset")
# Change index to show IncomeScreen
widget.setCurrentIndex(widget.currentIndex()-1)
# main
# if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
income = IncomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(income)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Also I'm using Python 3.7.4
EDIT: You can download the ui files here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有各种问题,但最重要的是,每次都称为,一个新的
distributionscreen
it 添加到堆叠widget,但是widget.setCurrentIndex(widget.currentIndex()+1)
将始终转到堆叠式窗口的 second index(这是 first em>您创建的实例)。可能的简单解决方法可能是使用
addwidget
返回的小部件的索引或使用setCurrentWidget
:不幸的是,虽然这会使您的代码工作,但它不是有效的解决方案,但由于还有其他重要问题,这些问题迟早会造成其他问题:
Incomemescreen.init_amount
一样);quiloader
作为主窗口的孩子加载小部件,而是不将其添加到布局(或设置为中央窗口小部件),这将使它无法在任何时候调整大小调整了顶级窗口:如果主窗口变得太小,则某些内容将看不到,如果它太大,则会有很多未使用的空间;widget
),而不能保证该变量是有效的;无论如何,它应该不是 是创建新小部件并设置堆叠式小部件的索引的实例,而是堆叠的小部件本身(或其任何祖先);除外:
),或者知道程序崩溃时出了什么问题;这是您的代码的可能修订(未经测试,因为您没有提供
ui
文件)。请注意:
pyside-uic
生成的文件,并使用多重继承方法,或切换到PYQT并使用其uic.loadui
current widget上的UI;There are various problems with your code, but the most important one is that every time
calculate
is called, a newDistributionScreen
is added to the stacked widget, butwidget.setCurrentIndex(widget.currentIndex()+1)
will always go to the second index of the stacked widget (which is the first instance you created).A possible simple workaround could be to use the index of the widget returned by
addWidget
or usesetCurrentWidget
:Unfortunately, while this would make your code work, it's not a valid solution, as there are other important issues that would create other problems sooner or later:
IncomeScreen.init_amount
);QUiLoader
to load the widget as a child of the main window, but without adding it to a layout (or setting as central widget), and this will make it unable to resize itself whenever the top level window is resized: if the main window becomes too small, some of the contents won't be visible, if it's too big there will be a lot of unused space;widget
) from an instance, while it's not guaranteed that the variable would be valid; in any case, it should not be the instance to create new widgets and set the index of the stacked widget, but the stacked widget itself (or any of its ancestors);except:
) or know what was wrong with your program if it crashes;This is a possible revision of your code (untested, as you didn't provide the
ui
files).Note that:
float()
done without previously checking if the string is actually a valid number);pyside-uic
and use the multiple inheritance method, or switch to PyQt and use itsuic.loadUi
which instead allows setting up the UI on the current widget;