繁体   English   中英

PyQt中组合框的存储值

[英]Store Value of Combo Box in PyQt

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

这是我的Example类的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM