如何调用函数

2024-10-01 02:26:51 发布

您现在位置: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')

Tags: 数据textself系统connectpage午餐str
1条回答
网友
1楼 · 发布于 2024-10-01 02:26:51

试试看:

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_())

enter image description here

相关问题 更多 >