我正在使用基于QWidget的PyQt开发应用程序。我有2个QWidget类。当我从ToolWindow类按下“选择”按钮时,我运行脚本并获取path_to_photo,我也切换到其他QWidget类。我想为QPixmap设置一个新路径,但是我只有在回到main并返回此窗口时才得到它。
class UIWindow(QWidget):
def __init__(self, parent=None):
super(UIWindow, self).__init__(parent)
self.vbox = QVBoxLayout(self)
global path_to_photo
self.ToolsBTN = QPushButton('text', self)
self.label = QLabel(self)
self.pixmap = QPixmap(path_to_photo)
self.label.setPixmap(self.pixmap)
self.resize(self.pixmap.width(),self.pixmap.height())
self.vbox.addWidget(self.ToolsBTN)
self.vbox.addWidget(self.label)
self.vbox.addStretch(5)
self.setLayout(self.vbox)
这是我的UIWindow,带有要显示的照片
class UIToolTab(QWidget):
def __init__(self, parent=None):
super(UIToolTab, self).__init__(parent)
self.vbox = QVBoxLayout(self)
self.CPSBTN = QPushButton("Choose", self)
self.vbox.addWidget(self.CPSBTN)
self.vbox.addStretch(5)
self.setLayout(self.vbox)
这是带有“选择”按钮的UIToolTab
class MainWindow(QMainWindow):
...
def startUIToolTab(self):
self.ToolTab = UIToolTab(self)
self.Window = UIWindow(self)
self.setWindowTitle("UIToolTab")
self.setCentralWidget(self.ToolTab)
self.ToolTab.CPSBTN.clicked.connect(self.startUIWindow)
self.show()
def startUIWindow(self):
global path_to_photo
self.Window = UIWindow(self)
file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
print(file)
path = Path(file)
glob_path = path.glob('*')
path_to_photo = brisquequality.answer(glob_path)
self.setWindowTitle("UIWindow")
self.setCentralWidget(self.Window)
self.Window.ToolsBTN.clicked.connect(self.startUIToolTab)
self.show()
切换功能
当我重写path_to_photo时如何更新QPixmap?
答案 0 :(得分:0)
该解决方案考虑了以下因素:
class UIWindow(QWidget):
def __init__(self, parent=None):
super(UIWindow, self).__init__(parent)
self.tools_btn = QPushButton("text", self)
self.label = QLabel(self)
vbox = QVBoxLayout(self)
vbox.addWidget(self.tools_btn)
vbox.addWidget(self.label)
vbox.addStretch()
def set_photo(self, photo):
pixmap = QPixmap(photo)
self.label.setPixmap(pixmap)
class UIToolTab(QWidget):
def __init__(self, parent=None):
super(UIToolTab, self).__init__(parent)
self.cps_btn = QPushButton("Choose", self)
vbox = QVBoxLayout(self)
vbox.addWidget(self.cps_btn)
vbox.addStretch()
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.stacked_widget = QStackedWidget()
self.setCentralWidget(self.stacked_widget)
self.ui_tool = UIToolTab()
self.ui_window = UIWindow()
self.stacked_widget.addWidget(self.ui_tool)
self.stacked_widget.addWidget(self.ui_window)
self.ui_tool.cps_btn.clicked.connect(self.on_choose_clicked)
self.ui_window.tools_btn.clicked.connect(self.open_tools_window)
@pyqtSlot()
def on_choose_clicked(self):
dirname = QFileDialog.getExistingDirectory(self, "Select Directory")
if dirname:
path = Path(dirname)
glob_path = path.glob("*")
path_to_photo = brisquequality.answer(glob_path)
print(path_to_photo)
self.ui_window.set_photo(path_to_photo)
self.stacked_widget.setCurrentIndex(1)
@pyqtSlot()
def open_tools_window(self):
self.stacked_widget.setCurrentIndex(0)