【问题标题】:Store Value of Combo Box in PyQt在 PyQt 中存储组合框的值
【发布时间】:2013-06-29 07:04:59
【问题描述】:

我正在制作一个简单的图形界面。我有两个名为calling_directory 和receiveing_directory 的表。我正在使用组合框显示值。现在我想知道,我选择了哪个值并且想将该值存储在一个变量中。我怎么做?

其次,我想使用一个重置按钮,它将组合框的值设置为默认值。我怎么做?

import re
import serial
import sqlite3
import os
import csv
import time
from subprocess import *
import serial.tools.list_ports
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import threading

global data
con_ports=[]
ports=[]
dial_ports=[]
receiving_ports=[]

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        btn1 = QtGui.QPushButton('Proceed', self)
        btn1.resize(btn1.sizeHint())
        btn1.move(460, 220)
        btn2 = QtGui.QPushButton('Reset',self)
        btn2.resize(btn2.sizeHint())
        btn2.move(5, 220)
        self.setGeometry(300,300,550,250)   
        self.setWindowTitle('Calling Functions')
        self.setWindowIcon(QtGui.QIcon('images.jpg'))

        self.lbl1 = QtGui.QLabel("Dialing Number", self)
        combo1 = QtGui.QComboBox(self)
        conn = sqlite3.connect('database')
        combo1.addItem('None')
        c = conn.cursor()
        c.execute('''select number from Calling_Directory''')
        rows = c.fetchall()
        for row in rows:
            calling_number=row[0]
            combo1.addItem(row[0])
            #calling_location=row[1]
            #x=x+120
        #conn.commit()
        combo1.move(10, 40)
        self.lbl1.move(10, 20)
        self.lbl1.adjustSize()  

        self.lbl2 = QtGui.QLabel("Receiving Number", self)
        combo2 = QtGui.QComboBox(self)
        #conn = sqlite3.connect('database')
        combo2.addItem('None')
        #c = conn.cursor()
        c.execute('''select number from Receiving_Directory''')
        rows2 = c.fetchall()
        for row in rows2:
            receiving_number=row[0]
            combo2.addItem(row[0])
            #calling_location=row[1]
                #x=x+120
        conn.commit()
        combo2.move(150, 40)
        self.lbl2.move(150, 20)

        self.lbl2.adjustSize()  
        self.show()

    def closeEvent(self,event):
        reply=QtGui.QMessageBox.question(self,'Mesage',"Are you sure you want to quit?",QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if(reply==QtGui.QMessageBox.Yes):
            event.accept()
        else:
            event.ignore()

def main():
    app = QtGui.QApplication(sys.argv)
    ex=Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python qt sqlite pyqt


    【解决方案1】:

    这是我的示例类的 init 方法的摘录,它可以执行您想要的操作。

    它演示了一种从 sql 调用填充组合框然后从组合框中检测用户选择的简单方法。

        cursor = db.cursor()   # declare cursor
    
        cursor.execute ("""
           select distinct(region) 
             from mytable
            where source = %s
         order by region
        """ , (mystring))
    
        result = cursor.fetchall()
    
        regionlist = [row[0] for row in result]    # use list comprehension to convert the tuple array into a list
    
        regionlist.insert(0,"*")    # prepend * to the list
    
        self.ui.region.insertItems(0,regionlist)   # set combobox values
    
        index = self.ui.region.findText("region7");   # get the corresponding index for specified string in combobox
        self.ui.region.setCurrentIndex(index)   # preselect a combobox value by index
    
        #---------------------------------------------
        # new style signal slot connections
        #---------------------------------------------
    
        self.ui.region.currentIndexChanged.connect(self.regionChanged)
    

    这是您可以添加到由上面的连接调用的示例类的方法。

    def ChangeRegion(self,index):
    
        index = self.ui.region.currentIndex()    # get current selection from combobox
    

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2011-07-10
      相关资源
      最近更新 更多