组合框在PyQt中的存储价值

时间:2013-06-29 07:04:59

标签: python qt sqlite pyqt

我正在制作一个简单的图形界面。我有两个名为calling_directory和receiving_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()

1 个答案:

答案 0 :(得分:0)

以下是我的示例类的 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)

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

def ChangeRegion(self,index):

    index = self.ui.region.currentIndex()    # get current selection from combobox