Python 使用QT设计器访问GUI的输入(组合框、旋转框、水平滑块)
我试着让这个代码尽可能清晰。我使用QT设计器获得了代码的基本内容 共有3个主要部分-访问输入(125-129)、设置按钮命令(41-45)和定义操作(155-168)(简化-打印输入) 目前的问题是,它在使用GUI时没有输出正确的值 例如,当您更改水平滑块,然后按下按钮时,它只打印“1”,即使它应该上升到“8” 我相信部分问题与“重译”有关,但我无法准确指出Python 使用QT设计器访问GUI的输入(组合框、旋转框、水平滑块),python,user-interface,Python,User Interface,我试着让这个代码尽可能清晰。我使用QT设计器获得了代码的基本内容 共有3个主要部分-访问输入(125-129)、设置按钮命令(41-45)和定义操作(155-168)(简化-打印输入) 目前的问题是,它在使用GUI时没有输出正确的值 例如,当您更改水平滑块,然后按下按钮时,它只打印“1”,即使它应该上升到“8” 我相信部分问题与“重译”有关,但我无法准确指出 ### I tried to make this code as clear as possible. I got the bare-bo
### I tried to make this code as clear as possible. I got the bare-bones of the code using QT Designer
### There are 3 main parts - accessing inputs (125-129), setting a command to the button (41-45), and defining the actions(155-168)(to simplify - print the inputs)
### The problem with it as it stands, is that it doesn't output the correct value when using the GUI
### For instance, when you change the horizontalSlider then press the button, it only prints "1", even though it should go up to "8"
### I believe part of the problem has something to do with the "retranslateUi", but I cannot pinpoint it...
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_StartWindow(object):
def setupUi(self, StartWindow):
global lives, speed, letters1, letters2, letters3 # need to makes these variables global in order to access them later on
StartWindow.setObjectName("StartWindow")
StartWindow.resize(260, 260)
StartWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(StartWindow)
self.centralwidget.setObjectName("centralwidget")
### this is the horizontal slider (speed)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setGeometry(QtCore.QRect(40, 150, 181, 22))
self.horizontalSlider.setMinimum(1) # This is the minimum (the value is 1)
self.horizontalSlider.setMaximum(8) # This is the maximum (the value is 8)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.PushButton = QtWidgets.QPushButton(self.centralwidget)
self.PushButton.setGeometry(QtCore.QRect(50, 180, 161, 51))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.PushButton.setFont(font)
self.PushButton.setCheckable(False)
self.PushButton.setObjectName("PushButton")
self.PushButton.clicked.connect(action1) # this is what tells the push button to print the lives (3-5)
self.PushButton.clicked.connect(action2) # this is what tells the push button to print the QWEletters ("Yes" or "No")
self.PushButton.clicked.connect(action3) # this is what tells the push button to print the ASDletters ("Yes" or "No")
self.PushButton.clicked.connect(action4) # this is what tells the push button to print the JKLletters ("Yes" or "No")
self.PushButton.clicked.connect(action5) # this is what tells the push button to print the speed (1-8)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 120, 141, 31))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 20, 61, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 50, 61, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 80, 61, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
### This is the comboBox (QWE)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 20, 41, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
### This is the comboBox (ASD)
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(100, 50, 41, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
### This is the comboBox (JKL)
self.comboBox_3 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_3.setGeometry(QtCore.QRect(100, 80, 41, 22))
self.comboBox_3.setObjectName("comboBox_3")
self.comboBox_3.addItem("")
self.comboBox_3.addItem("")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(160, 30, 91, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
### This is the spinBox (lives)
self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox.setGeometry(QtCore.QRect(170, 60, 71, 31))
self.spinBox.setMinimumSize(QtCore.QSize(61, 0))
font = QtGui.QFont()
font.setPointSize(15)
self.spinBox.setFont(font)
self.spinBox.setMinimum(3) # this sets the minimum value for the lives (3)
self.spinBox.setMaximum(5) # this sets the maximum value for the lives (5)
self.spinBox.setObjectName("spinBox")
### Gets the inputs for the qwe, slider, and spinbox
lives = int(self.spinBox.value()) # spinBox (lives)
letters1 = self.comboBox.currentText() # comboBox (QWE)
letters2 = self.comboBox_2.currentText() # comboBox (ASD)
letters3 = self.comboBox_3.currentText() # comboBox (JKL)
speed = self.horizontalSlider.sliderPosition() # horizontalSlider (speed)
StartWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(StartWindow)
self.statusbar.setObjectName("statusbar")
StartWindow.setStatusBar(self.statusbar)
self.retranslateUi(StartWindow)
QtCore.QMetaObject.connectSlotsByName(StartWindow)
def retranslateUi(self, StartWindow):
_translate = QtCore.QCoreApplication.translate
StartWindow.setWindowTitle(_translate("StartWindow", "Start Window"))
self.PushButton.setText(_translate("StartWindow", "Start!"))
self.label.setText(_translate("StartWindow", "Speed (1-8) :"))
self.label_2.setText(_translate("StartWindow", "QWE - "))
self.label_3.setText(_translate("StartWindow", " ASD - "))
self.label_4.setText(_translate("StartWindow", " JKL - "))
self.comboBox.setItemText(0, _translate("StartWindow", "Yes"))
self.comboBox.setItemText(1, _translate("StartWindow", "No"))
self.comboBox_2.setItemText(0, _translate("StartWindow", "Yes"))
self.comboBox_2.setItemText(1, _translate("StartWindow", "No"))
self.comboBox_3.setItemText(0, _translate("StartWindow", "Yes"))
self.comboBox_3.setItemText(1, _translate("StartWindow", "No"))
self.label_5.setText(_translate("StartWindow", "# Lives :"))
def action1(): # This is to access the lives -- spinBox
print(lives)
def action2(): # This is to access the letters (QWE) -- comboBox
print(letters1)
def action3(): # This is to access the letters (ASD) -- comboBox
print(letters2)
def action4(): # This is to access the letters (JKL) -- comboBox
print(letters3)
def action5(): # This is to access the speed -- horizontalSlider
print(speed)
# if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
StartWindow = QtWidgets.QMainWindow()
ui = Ui_StartWindow()
ui.setupUi(StartWindow)
StartWindow.show()
sys.exit(app.exec_())
如果你能帮上忙,那就太好了。这是一个正在进行的项目,我正试图将其转化为一种学习体验。到目前为止我很喜欢,但我还有很多东西要学。。谢谢大家! 希望这就是你想要的。我目前仍在学习python,但我相信您的函数试图访问一个类变量,但您没有正确调用该变量。即使将变量设置为全局变量,它们对于类范围也是全局的。我将函数移动到类中,对它们进行了一些调整,以便在调用函数时捕获comboBox、spinBox和horizontalSlider的值
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_StartWindow(object):
def setupUi(self, StartWindow):
# need to makes these variables global in order to access them later on
global lives, speed, letters1, letters2, letters3
StartWindow.setObjectName("StartWindow")
StartWindow.resize(260, 260)
StartWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(StartWindow)
self.centralwidget.setObjectName("centralwidget")
### this is the horizontal slider (speed)
self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
self.horizontalSlider.setGeometry(QtCore.QRect(40, 150, 181, 22))
# This is the minimum (the value is 1)
self.horizontalSlider.setMinimum(1)
# This is the maximum (the value is 8)
self.horizontalSlider.setMaximum(8)
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName("horizontalSlider")
self.PushButton = QtWidgets.QPushButton(self.centralwidget)
self.PushButton.setGeometry(QtCore.QRect(50, 180, 161, 51))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.PushButton.setFont(font)
self.PushButton.setCheckable(False)
self.PushButton.setObjectName("PushButton")
# this is what tells the push button to print the lives (3-5)
self.PushButton.clicked.connect(self.action1)
# this is what tells the push button to print the QWEletters ("Yes" or "No")
self.PushButton.clicked.connect(self.action2)
# this is what tells the push button to print the ASDletters ("Yes" or "No")
self.PushButton.clicked.connect(self.action3)
# this is what tells the push button to print the JKLletters ("Yes" or "No")
self.PushButton.clicked.connect(self.action4)
# this is what tells the push button to print the speed (1-8)
self.PushButton.clicked.connect(self.action5)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 120, 141, 31))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 20, 61, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(30, 50, 61, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 80, 61, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
### This is the comboBox (QWE)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 20, 41, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
### This is the comboBox (ASD)
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(100, 50, 41, 22))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
### This is the comboBox (JKL)
self.comboBox_3 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_3.setGeometry(QtCore.QRect(100, 80, 41, 22))
self.comboBox_3.setObjectName("comboBox_3")
self.comboBox_3.addItem("")
self.comboBox_3.addItem("")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(160, 30, 91, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
### This is the spinBox (lives)
self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox.setGeometry(QtCore.QRect(170, 60, 71, 31))
self.spinBox.setMinimumSize(QtCore.QSize(61, 0))
font = QtGui.QFont()
font.setPointSize(15)
self.spinBox.setFont(font)
# this sets the minimum value for the lives (3)
self.spinBox.setMinimum(3)
# this sets the maximum value for the lives (5)
self.spinBox.setMaximum(5)
self.spinBox.setObjectName("spinBox")
### Gets the inputs for the qwe, slider, and spinbox
# spinBox (lives)
# lives = int(self.spinBox.value())
# letters1 = self.comboBox.currentText() # comboBox (QWE)
# letters2 = self.comboBox_2.currentText() # comboBox (ASD)
# letters3 = self.comboBox_3.currentText() # comboBox (JKL)
# horizontalSlider (speed)
# speed = self.horizontalSlider.sliderPosition()
StartWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(StartWindow)
self.statusbar.setObjectName("statusbar")
StartWindow.setStatusBar(self.statusbar)
self.retranslateUi(StartWindow)
QtCore.QMetaObject.connectSlotsByName(StartWindow)
def retranslateUi(self, StartWindow):
_translate = QtCore.QCoreApplication.translate
StartWindow.setWindowTitle(_translate("StartWindow", "Start Window"))
self.PushButton.setText(_translate("StartWindow", "Start!"))
self.label.setText(_translate("StartWindow", "Speed (1-8) :"))
self.label_2.setText(_translate("StartWindow", "QWE - "))
self.label_3.setText(_translate("StartWindow", " ASD - "))
self.label_4.setText(_translate("StartWindow", " JKL - "))
self.comboBox.setItemText(0, _translate("StartWindow", "Yes"))
self.comboBox.setItemText(1, _translate("StartWindow", "No"))
self.comboBox_2.setItemText(0, _translate("StartWindow", "Yes"))
self.comboBox_2.setItemText(1, _translate("StartWindow", "No"))
self.comboBox_3.setItemText(0, _translate("StartWindow", "Yes"))
self.comboBox_3.setItemText(1, _translate("StartWindow", "No"))
self.label_5.setText(_translate("StartWindow", "# Lives :"))
# This is to access the lives -- spinBox
def action1(self):
lives = int(self.spinBox.value())
print(lives)
# This is to access the letters (QWE) -- comboBox
def action2(self):
letters1 = self.comboBox.currentText()
if letters1 == 'Yes':
print('QWE')
# This is to access the letters (ASD) -- comboBox
def action3(self):
letters2 = self.comboBox_2.currentText()
if letters2 == 'Yes':
print('ASD')
# This is to access the letters (JKL) -- comboBox
def action4(self):
letters3 = self.comboBox_3.currentText()
if letters3 == 'Yes':
print('JKL')
# This is to access the speed -- horizontalSlider
def action5(self):
speed = self.horizontalSlider.sliderPosition()
print(speed)
# if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
StartWindow = QtWidgets.QMainWindow()
ui = Ui_StartWindow()
ui.setupUi(StartWindow)
StartWindow.show()
sys.exit(app.exec_())