Reputation: 2602
I am making Multi-Page application in PyQt4, so whenever user does specific action (clicking a button for example) there is an update in widgets.
For example, There are 5 widgets and one button:
3 widgets are hidden, 2 widgets are shown.
Whenever i click the button, it will hide 2 widgets, and show those 3.
So in code, it should be something like this:
# startup
def somefunc(self):
widget1 = QtGui.QLabel("Widget1", self)
widget2 = QtGui.QLabel("Widget2", self)
widget3 = QtGui.QLabel("Widget3", self)
widget4 = QtGui.QLabel("Widget4", self)
widget5 = QtGui.QLabel("Widget5", self)
widget1.setHidden()
widget2.setHidden()
widget3.setHidden()
widget4.show()
widget5.show()
btn = QtGui.QPushButton("Click", self)
btn.clicked.connect(self.SomeotherFunc)
# My Question: (Code down below doesn't work, it's for example)
def SomeotherFunc(self):
self.somefunc.widget1.Show()
self.somefunc.widget1.Show()
self.somefunc.widget1.Show()
self.somefunc.widget4.setHidden()
self.somefunc.widget5.setHidden()
import sys
from PyQt4 import QtGui, QtCore
import resources
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(0, 0, 1280, 800)
self.setWindowTitle("E.S Quiz")
self.home()
def home(self):
pic = QtGui.QLabel(self)
pic.setGeometry(0, 0, 1280, 800)
pic.setPixmap(QtGui.QPixmap(":/images/background.png"))
btn = QtGui.QPushButton("", self)
btn.resize(150, 120)
btn.move(600, 400)
btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
btn.setObjectName('btn')
btn.setStyleSheet("#btn {background-image: url(':/images/Button1.png'); border: none; }"
"#btn:hover { background-image: url(':/images/Button1Hover.png'); }"
"#btn:pressed { background-image: url(':/images/Button1Press.png'); }")
btn.clicked.connect(self.test)
self.show()
def test(self):
print "Here"
def startup():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
startup()
How do i modify some functions widgets from another function?
Upvotes: 1
Views: 1889
Reputation: 9620
I've edited your code. I believe this will do the job. Just push the button, and see the label disappear. Have fun :-)
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(40, 80, 1280, 800)
# You should make a 'mainWidget' that
# serves as a container for all your other
# widgets.
self.mainWidget = QtGui.QWidget()
self.setCentralWidget(self.mainWidget)
self.setWindowTitle("E.S Quiz")
self.home()
self.show()
def home(self):
# Make the label
pic = QtGui.QLabel(parent = self.mainWidget)
pic.setText("My label")
pic.setGeometry(0, 0, 1280, 800)
#pic.setPixmap(QtGui.QPixmap(":/images/background.png"))
# Make a label that will disappear when
# you push the button.
self.myLabel = QtGui.QLabel(parent = self.mainWidget)
self.myLabel.setText("My removable label")
self.myLabel.setGeometry(40,40,200,100)
# Make the button
self.btn = QtGui.QPushButton(parent = self.mainWidget)
self.btn.setCheckable(True)
self.btn.setText("My button")
self.btn.resize(150, 120)
self.btn.move(600, 400)
self.btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.btn.setObjectName('btn')
# self.btn.setStyleSheet("#btn {background-image: url(':/images/Button1.png'); border: none; }"
# "#btn:hover { background-image: url(':/images/Button1Hover.png'); }"
# "#btn:pressed { background-image: url(':/images/Button1Press.png'); }")
self.btn.clicked.connect(self.btnAction)
def btnAction(self, pressed):
print("Button pushed")
if(pressed):
self.myLabel.setVisible(False)
else:
self.myLabel.setVisible(True)
def startup():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
startup()
Upvotes: 1
Reputation: 37489
You need to store references to the subwidgets on the main window using self
def func(self):
self.btn = QPushButton(...)
...
def other_func(self):
self.btn.setText('Hello')
Upvotes: 1