gpt4 book ai didi

python - 从 pyqt4 中的垂直 slider 获取值并将该值保存到一个变量中

转载 作者:行者123 更新时间:2023-12-01 04:59:23 24 4
gpt4 key购买 nike

我是 qt 和 python 世界的新手。我在 pyqt4 中创建了垂直 slider 。我想访问垂直 slider (int)值并将其分配给一个变量,因为我必须将该变量的值传递给另一个函数。这是我的代码,请建议我应该在代码中添加什么,以便从 slider 中获取值(value)。

代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Thu Sep 25 13:08:56 2014
# by: PyQt4 UI code generator 4.9.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(698, 455)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.verticalSlider = QtGui.QSlider(self.centralWidget)
self.verticalSlider.setGeometry(QtCore.QRect(100, 70, 19, 251))
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName(_fromUtf8("verticalSlider"))
self.lcdNumber = QtGui.QLCDNumber(self.centralWidget)
self.lcdNumber.setGeometry(QtCore.QRect(250, 130, 211, 81))
self.lcdNumber.setObjectName(_fromUtf8("lcdNumber"))
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 698, 21))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)

self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.verticalSlider, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.lcdNumber.display)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

最佳答案

假设您的代码中有以下内容:

sld = QtGui.QSlider(QtCore.Qt.Vertical, self)

您可以将 slider 中的更改连接到其他功能,例如:

sld.valueChanged[int].connect(self.funcThatTakeAnIntAsArg)

并定义方法

def funcThatTakeAnIntAsArg(self, valueOfSlider):
self.someWidget.doSomethingWith(valueOfSlider)

作为QSlider继承者QAbstractSlider,你可以得到 valueQSlider.value

关于python - 从 pyqt4 中的垂直 slider 获取值并将该值保存到一个变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com