在PyQt中,类的多个实例(slider+label)相互阻塞。

问题描述 投票:0回答:1

在我的程序中,我想在PyQt中拥有带有多个带标签的滑块的UI,用于收集输入数据。

它们非常相似,所以我做了一个类来轻松创建它们。

类的代码。

    class MySlider(QWidget):
        def make_slider(self, name, posx, posy, maxi, mini, font_size, orientation):
            self.label = QLabel(name, parent=self)
            self.label.setGeometry(posx - 30, posy - 45, 100, 40)  
            self.label.setFont(QtGui.QFont("Sanserif", font_size))
            self.slider = QSlider(parent=self)
            self.slider.setOrientation(orientation)
            self.slider.setTickPosition(QSlider.TicksBelow)
            self.slider.setMaximum(maxi)
            self.slider.setMinimum(mini)
            self.slider.setTickInterval(1)
            self.slider.move(posx, posy)
            self.label2 = QLabel(str(mini), parent=self)
            self.label2.setFont(QtGui.QFont("Sanserif", font_size))
            self.label2.setGeometry(posx, posy + 90, 50, 20)
            self.slider.valueChanged.connect(self.changed_value)

        def changed_value(self):
            wartosc = self.slider.value()
            self.label2.setText(str(wartosc))

然后我在MainWindow中创建了3个实例。

            self.bumping_slider = MySlider(self)
            self.bumping_slider.make_slider(("     Kołek\nbumpingowy"), 500, 95, 10, 1, 12, Qt.Vertical)

            self.column_slider = MySlider(self)
            self.column_slider.make_slider(("   Liczba\n   kolumn"), 590, 95, 6, 1, 12, Qt.Vertical)

            self.test_slider = MySlider(self)
            self.test_slider.make_slider(("   Liczba\n   czegos"), 680, 95, 6, 1, 12, Qt.Vertical)

当我直接在MainWindow类定义中写代码时,滑块可以正常工作。但是当我使用我创建的类时,问题是changed_value函数总是只对最后一个滑块有效。其他的都被阻止了,我甚至不能在应用程序窗口中移动滑块并改变它的值。应用程序运行时没有任何错误。我找不到问题出在哪里,所以函数changed_value将为每个实例单独工作。

完整的代码。


import sys

from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MySlider(QWidget):
    def __init__(self, parent=None):
        super(MySlider, self).__init__(parent)

    def make_slider(self, name, posx, posy, maxi, mini, font_size, orientation):
        """posx,posy,font_size,max,min = int orientation = Qt.Horizontal or Vertical,name = str name of slider"""
        self.label = QLabel(name, self)
        self.label.setGeometry(posx - 30, posy - 45, 100, 40)  # 210,60
        self.label.setFont(QtGui.QFont("Sanserif", font_size))
        self.slider = QSlider(self)
        self.slider.setOrientation(orientation)
        self.slider.setTickPosition(QSlider.TicksBelow)
        self.slider.setMaximum(maxi)
        self.slider.setMinimum(mini)
        self.slider.setTickInterval(1)
        self.slider.move(posx, posy)
        self.label2 = QLabel(str(mini), self)
        self.label2.setFont(QtGui.QFont("Sanserif", font_size))
        self.label2.setGeometry(posx, posy + 90, 50, 20)
        self.slider.valueChanged.connect(self.changed_value)

    def changed_value(self):
        print(self)
        wartosc = self.slider.value()
        self.label2.setText(str(wartosc))


class Window(QWidget):
    """kreowanie klasy okna"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Kalkulator liczby kombinacji")
        self.setGeometry(1700, 600, 750, 320)
        self.setWindowIcon(QtGui.QIcon("icon.png"))

        self.test_slider2 = MySlider(self)
        self.test_slider2.make_slider(("   Liczba\n   czegos"), 140, 95, 6, 1, 12, Qt.Vertical)

        self.test_slider3 = MySlider(self)
        self.test_slider3.make_slider(("   Liczba\n   czegos"), 230, 95, 6, 1, 12, Qt.Vertical)

        self.test_slider4 = MySlider(self)
        self.test_slider4.make_slider(("   Liczba\n   czegos"), 320, 95, 6, 1, 12, Qt.Vertical)

        self.test_slider5 = MySlider(self)
        self.test_slider5.make_slider(("   Liczba\n   czegos"), 410, 95, 6, 1, 12, Qt.Vertical)

        self.bumping_slider = MySlider(self)
        self.bumping_slider.make_slider(("     Kołek\nbumpingowy"), 500, 95, 10, 1, 12, Qt.Vertical)

        self.column_slider = MySlider(self)
        self.column_slider.make_slider(("   Liczba\n   kolumn"), 590, 95, 6, 1, 12, Qt.Vertical)

        self.test_slider = MySlider(self)
        self.test_slider.make_slider(("   Liczba\n   czegos"), 680, 95, 6, 1, 12, Qt.Vertical)

        self.show()
app = QApplication(sys.argv)
app.setStyle("Fusion")
window = Window()
sys.exit(app.exec())

python python-3.x pyqt pyqt5
1个回答
1
投票

将你的小部件粘贴到布局中。

import sys

from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MySlider(QWidget):
    def __init__(self, name, maxi, mini, font_size, orientation, parent=None):
        super(MySlider, self).__init__(parent)
        self.setMinimumSize(100, 200)                                          # +++

#    def make_slider(self, name, posx, posy, maxi, mini, font_size, orientation):
#        """posx,posy,font_size,max,min = int orientation = Qt.Horizontal or Vertical,name = str name of slider"""

        self.label = QLabel(name, self)
#        self.label.setGeometry(posx - 30, posy - 45, 100, 40)  # 210,60
        self.label.setFont(QtGui.QFont("Sanserif", font_size))
        self.slider = QSlider(self)
        self.slider.setOrientation(orientation)
        self.slider.setTickPosition(QSlider.TicksBelow)
        self.slider.setMaximum(maxi)
        self.slider.setMinimum(mini)
        self.slider.setTickInterval(1)
#        self.slider.move(posx, posy)
        self.label2 = QLabel(str(mini), self)
        self.label2.setFont(QtGui.QFont("Sanserif", font_size))
#        self.label2.setGeometry(posx, posy + 90, 50, 20)
        self.slider.valueChanged.connect(self.changed_value)
# ++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv        
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.label, alignment = Qt.AlignCenter)
        self.layout.addWidget(self.slider, alignment = Qt.AlignCenter)
        self.layout.addWidget(self.label2, alignment = Qt.AlignCenter)
        self.layout.addStretch()
# ++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    def changed_value(self, val):                                             # + val
        print(val)
#        wartosc = self.slider.value()
#        self.label2.setText(str(wartosc))
        self.label2.setNum(val)                                               # + val


class Window(QWidget):
#    """kreowanie klasy okna"""

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Kalkulator liczby kombinacji")
#        self.setGeometry(100, 60, 750, 320)
        self.resize(750, 300)
        self.setWindowIcon(QtGui.QIcon("icon.png"))

        self.layout = QHBoxLayout(self)                                              # +++
        self.layout.addStretch()

        self.test_slider2 = MySlider("Liczba\nczegos", 6, 1, 12, Qt.Vertical, self)
#        self.test_slider2.make_slider(("   Liczba\n   czegos"), 140, 95, 6, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.test_slider2)                                      # +++


        self.test_slider3 = MySlider("Liczba\nczegos",6, 1, 12, Qt.Vertical, self)
#        self.test_slider3.make_slider(("   Liczba\n   czegos"), 230, 95, 6, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.test_slider3)                                      # +++

        self.test_slider4 = MySlider("Liczba\nczegos",6, 1, 12, Qt.Vertical, self)
#        self.test_slider4.make_slider(("   Liczba\n   czegos"), 320, 95, 6, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.test_slider4)                                      # +++

        self.test_slider5 = MySlider("Liczba\nczegos",6, 1, 12, Qt.Vertical, self)
#        self.test_slider5.make_slider(("   Liczba\n   czegos"), 410, 95, 6, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.test_slider5)                                      # +++

        self.bumping_slider = MySlider("Kołek\nbumpingowy", 10, 1, 12, Qt.Vertical, self)
#        self.bumping_slider.make_slider(("     Kołek\nbumpingowy"), 500, 95, 10, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.bumping_slider)                                      # +++

        self.column_slider = MySlider("Liczba\nkolumn", 6, 1, 12, Qt.Vertical, self)
#        self.column_slider.make_slider(("   Liczba\n   kolumn"), 590, 95, 6, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.column_slider)                                      # +++

        self.test_slider = MySlider("Liczba\nczegos", 6, 1, 12, Qt.Vertical, self)
#        self.test_slider.make_slider(("   Liczba\n   czegos"), 680, 95, 6, 1, 12, Qt.Vertical)
        self.layout.addWidget(self.test_slider)                                      # +++

        self.layout.addStretch()


if __name__ == '__main__': 
    app = QApplication(sys.argv)
    app.setStyle("Fusion")
    window = Window()
    window.show()
    sys.exit(app.exec())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.