1

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()

Full Code:

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()

Question:

How do i modify some functions widgets from another function?

6
  • 1
    Could you post your entire GUI code? That would make it easier to help you. Commented Apr 19, 2016 at 19:27
  • Thanks for suggesting @K.Mulier Updated post. Commented Apr 19, 2016 at 19:40
  • 1
    maybe somefunc should be a class. Commented Apr 19, 2016 at 19:40
  • Both are inside class @Thomas Commented Apr 19, 2016 at 19:41
  • 1
    the widget in somefunc are locals. to access them in another method you must make them class members. Commented Apr 19, 2016 at 19:43

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

4 Comments

I was doing it like this: btn = QtGui.QPushButton("text", self) is there any difference?
@ShellRox Yes, it is different
thanks i will check this out soon when im back, i think this is solution.
Worked without any problems! Thanks!
1

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()

2 Comments

Hi @ShellRox , please let me know if this answer has helped you out :-)
Thank you @ShellRox.. It is up to you which one you will mark as solved. The other guy has 5000 points, I have only 129. So you can choose for the underdog if you wish ;-) But in the end, it's up to you of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.