【问题标题】:How to replace the contents in a frame on button click in Pyqt如何在Pyqt中单击按钮时替换框架中的内容
【发布时间】:2015-01-12 01:36:39
【问题描述】:

所以我有两个框架,第一个框架包含所有标题元素,第二个框架包含一个带有两个按钮的图像。这是我第一个屏幕的内容。现在,当我单击按钮时,第二帧中的内容应替换为具有属性列表的 .py 文件。第一帧内容在所有屏幕中都相同,只有第二帧应该改变。这是我在第一个屏幕上的代码:

import sys
import os
import taurus
from PyQt4 import QtCore, QtGui, uic
from taurus.external.qt import Qt
from taurus.qt.qtgui.panel import TaurusForm
from taurus.qt.qtgui.application import TaurusApplication

form_class = uic.loadUiType("FirstScreen.ui")[0]                 # Load the UI
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class MyWindowClass(QtGui.QMainWindow, form_class):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        #self.editValue.clicked.connect(self.openForm_clicked)
        self.setWindowTitle('FirstScreen')
        self.button1.setStyleSheet("QPushButton { background-color:#00FFFF;}")
        self.dateTime.setText(QtCore.QDate.currentDate().toString())
        self.timeLabel.setText(QtCore.QTime.currentTime().toString())
        self.button2.setStyleSheet("QPushButton { background-color:#00FFFF;}")
        myPixmap = QtGui.QPixmap(_fromUtf8('Sample.png'))
        myPixmap=myPixmap.scaledToHeight(100)
        self.pic.setPixmap(myPixmap)





app = QtGui.QApplication(sys.argv)
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()

您能否告诉我如何在单击按钮时使用 .py 文件加载第二帧。我尝试加载的另一个 .py 文件也是使用 Pyqt 创建的。

【问题讨论】:

    标签: python pyqt pyqt4


    【解决方案1】:

    您可以使用signals and slots。即使用 clicked 信号并调用一个函数,例如:onClick 。

    self.button_name.clicked.connect(self.onClick)

    在函数调用中

                self.second_frame = second_ui_class()
                self.second_frame.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-03
      • 2020-03-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多