python - 如何调用函数
问题描述
我必须组合框;组合框 1 和组合框 2。我想从两个组合框中检索值以获得是“早餐”还是“午餐”的结果
Combobox1 将获取小时的数据,例如 07,08,09,10 combobox2 将获取分钟的数据,例如 30,59 等
预期的结果是系统获取这两个值并确定是早餐还是午餐。一个例子是 07 和 59,它是早上 7:59,它是早餐。
我有两个功能分别打印用户选择的值...我想打印“您选择了'07:59'”另外,我希望系统识别它是早餐,所以我可以打开当我进入下一页时,早餐页。
page.comboBox.addItems(selecthour)
page.comboBox.activated[str].connect(self.onComboActivated)
page.comboBox.setGeometry(150,30,105,40)
page.comboBox2.addItems(selectmin)
page.comboBox2.activated[str].connect(self.onCombo2Activated)
page.comboBox2.setGeometry(280,30,105,40)
def onCombo2Activated(self, text):
print("choose time: {}".format(text))
if 800<= int(text) <= 1200:
print('Hello')
解决方案
试试看:
import sys
from PyQt5 import QtWidgets
class Main(QtWidgets.QDialog):
def __init__(self):
super(Main, self).__init__()
selecthour = [ '{:>02}'.format(i) for i in range(6, 23)]
selectmin = [ '{:>02}'.format(i) for i in range(0, 60)]
self.comboBox = QtWidgets.QComboBox(self)
self.comboBox.addItems(selecthour)
self.comboBox.activated[str].connect(lambda ch, c='hour': self.onComboActivated(ch, c))
self.comboBox.setGeometry(150, 30, 105, 40)
self.comboBox2 = QtWidgets.QComboBox(self)
self.comboBox2.addItems(selectmin)
self.comboBox2.activated[str].connect(lambda ch, c='min': self.onComboActivated(ch, c))
self.comboBox2.setGeometry(280,30,105,40)
def onComboActivated(self, text, c):
print("\nchoose time: {} - {}".format(text, c))
print("current time: {}:{}".format(self.comboBox.currentText(), self.comboBox2.currentText()))
text = "{}{}".format(self.comboBox.currentText(), self.comboBox2.currentText())
if '0730' <= text <= '1130':
print('Hello, breakfast')
elif '1131' <= text <= '1600':
print('Hello, lunch')
elif '1601' <= text <= '1900':
print('Hello, supper')
else:
print('It’s harmful to eat at this time.')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())