简体   繁体   中英

Issue about PyQt5

I am trying to create a simple interface like this:

from PyQt5 import QtWidgets,QtGui
class program():

    def __init__(self):

       self.window = QtWidgets.QWidget()
       self.window.setWindowTitle("how many click")

       self.text = QtWidgets.QLabel(self.window)
       self.text.setText(("not clicked"))     
       self.text.setGeometry(240,200,300,50)

       self.text2 = QtWidgets.QLabel(self.window)

       self.picture = QtWidgets.QLabel(self.window)

       self.button=QtWidgets.QPushButton(self.window)
       self.button.setText("click")
       self.button.setFont(QtGui.QFont('',10))
       self.button.setGeometry(250,100,200,50)

       self.window.setGeometry(600,200,800,600)

       self.window.show()

       self.count=0

       self.button.clicked.connect(self.click)

     def click(self):
       self.count+= 1
       self.text.setText(((f"you clicked {self.count} times")))
       self.text.setFont(QtGui.QFont('',10))

       if  self.count == 5:

          self.text2.setText(("You clicked too much"))
          self.text2.setGeometry(250, 250, 300, 50)
          self.picture.setPixmap(QtGui.QPixmap("C:/Users/Administrator/Desktop/mypic.png"))
          self.picture.move(300, 300)

 app = QtWidgets.QApplication(sys.argv)

 run= program()

 sys.exit(app.exec_())

In this code my picture appears when I click 5 times to button but picture becomes very tiny as in pic1. However when I write setPixmap and picture.move codes into init function picture becomes normal size as in pic2.

pic1:
图1 pic2:
图2

The simple solution to your issue is to add the following line after setting the pixmap:

self.picture.adjustSize()

The direct reason is that when when the widget is shown the label has not yet a pixmap, so its geometry is already set to its minimum size (defaults to 100x30). Then, when the pixmap is set, the label doesn't automatically update its size.

The logical reason is that you are using fixed geometries for your widget, and this approach is generaly discouraged for lots of reasons, with the most important being the fact that elements within a window should always adapt their geometries (size and position) to the size of the parent, possibly by occupying all the available space and preventing the elements to become invisible if the window is resized to a smaller size.

To avoid that, you should always use layout managers (in your case, a QVBoxLayout could be enough).

For example:

class program():

    def __init__(self):

        self.window = QtWidgets.QWidget()
        # ...
        layout = QtWidgets.QVBoxLayout(self.window)
        layout.addWidget(self.text)
        layout.addWidget(self.text2)
        layout.addWidget(self.picture)
        layout.addWidget(self.button)

        # it's good practice to always show the window *after* adding all elements
        self.window.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM