PyQt5–创建字符串旋转盒
在这篇文章中,我们将看到如何创建一个有字符串值的旋转框,在默认情况下,旋转框只包含整数值。字符串旋转框是一个有字符串值的旋转框,可以从给定的字符串值中选择。
为了创建一个字符串旋钮箱,我们必须创建一个继承于QSpinBox类的自定义类,下面是字符串旋钮箱类的语法
# custom class for String Spin Box
class StringBox(QSpinBox):
# constructor
def __init__(self, parent=None):
super(StringBox, self).__init__(parent)
# string values
strings = ["a", "b", "c", "d", "e", "f", "g"]
# calling setStrings method
self.setStrings(strings)
# method setString
# similar to set value method
def setStrings(self, strings):
# making strings list
strings = list(strings)
# making tuple from the string list
self._strings = tuple(strings)
# creating a dictionary
self._values = dict(zip(strings, range(len(strings))))
# setting range to it the spin box
self.setRange(0, len(strings)-1)
# overwriting the textFromValue method
def textFromValue(self, value):
# returning string from index
# _string = tuple
return self._strings[value]
解释: 这个类的基本思想是,我们创建一个继承自旋盒类的自定义类,这就是为什么它具有普通自旋盒的所有能力,但它不显示整数,而是显示字符串值。我们已经创建了字典,每个字符串都有一个整数键,旋转框的范围是字符串值的数量,在覆盖textFromValue方法的帮助下,它显示字符串而不是整数。
下面是实现的过程
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
# custom class for String Spin Box
class StringBox(QSpinBox):
# constructor
def __init__(self, parent = None):
super(StringBox, self).__init__(parent)
# string values
strings = ["a", "b", "c", "d", "e", "f", "g"]
# calling setStrings method
self.setStrings(strings)
# method setString
# similar to set value method
def setStrings(self, strings):
# making strings list
strings = list(strings)
# making tuple from the string list
self._strings = tuple(strings)
# creating a dictionary
self._values = dict(zip(strings, range(len(strings))))
# setting range to it the spin box
self.setRange(0, len(strings)-1)
# overwriting the textFromValue method
def textFromValue(self, value):
# returning string from index
# _string = tuple
return self._strings[value]
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 600, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating a string spin box
string_spin_box = StringBox(self)
# setting geometry to the spin box
string_spin_box.setGeometry(100, 100, 200, 40)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())