PDA

View Full Version : Copy a .txt file in a Spinbox (PyQT)



dhoomh
14th April 2014, 11:52
Hi everyone,

I just want to copy elements in a text file, to spinbox. For do this, you need to clic "LOAD SGV" and the function is "F_Action1"
So i don't know how proceed, need your help please.

Thats my code :


#!/usr/bin/env python
# -*- coding: cp1252 -*-
#SPEC 0.1

import sys
import time
import serial
import binascii

from PyQt4 import QtCore, QtGui

class MyPopup(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)

''' Titre_1 '''
self.Label_1 = QtGui.QLabel()
self.Label_1.setText("NUMERO DE PORT")

''' Titre_11 '''
self.Label_11 = QtGui.QLabel()
self.Label_11.setText("VITESSE")

''' ComboBox_11 '''
self.ComboBox_11 = QtGui.QComboBox()
self.ComboBox_11.setGeometry(QtCore.QRect(10,10,10 ,10))
self.ComboBox_11.addItem("COM 1")
self.ComboBox_11.addItem("COM 25")
self.ComboBox_11.addItem("COM 2")
self.ComboBox_11.addItem("COM 4")

''' ComboBox_22 '''
self.ComboBox_22 = QtGui.QComboBox()
self.ComboBox_22.setGeometry(QtCore.QRect(10,10,10 ,10))
self.ComboBox_22.addItem("4800")
self.ComboBox_22.addItem("115200")

self.bouton1 = QtGui.QPushButton(u"Open", self)
self.bouton1.clicked.connect(self.F_Open)

self.bouton2 = QtGui.QPushButton(u"Close", self)
self.bouton2.clicked.connect(self.F_Close)

# positionner les widgets dans la fenêtre
posit = QtGui.QGridLayout()
posit.addWidget(self.Label_1, 0, 0)
posit.addWidget(self.ComboBox_11, 1, 0)
posit.addWidget(self.Label_11, 2, 0)
posit.addWidget(self.ComboBox_22, 3, 0)
posit.addWidget(self.bouton1, 4, 0)
posit.addWidget(self.bouton2, 5, 0)
self.setLayout(posit)


# SERIAL #

def F_Open(self):
F_Serial(1,self.ComboBox_11.itemText(self.ComboBox _11.currentIndex()),self.ComboBox_22.itemText(self .ComboBox_22.currentIndex()))


def F_Close(self):
F_Serial(4,"null", "null")


def M_Signal(self):
self.M_Status(3)
time.sleep(0.2)
self.M_Status(2)


def F_Serial(a,port_ou_msg,vitesse):
s = "null"
if a == 1:
num_port = int(port_ou_msg[4:6])
vit = int(vitesse)
global ser
ser = serial.Serial(num_port-1,vit,timeout=0,parity='N', rtscts=1)
print ser.portstr # check which port was really used
elif a == 2:
ser.write(port_ou_msg + "\r")
elif a == 3:
s = ser.readline(120)
elif a == 4:
ser.close()
return s



class essai(QtGui.QMainWindow):
def __init__(self):

# Main Window #
QtGui.QMainWindow.__init__(self)
self.resize(310,310)
self.setWindowTitle(' Travaille ')

# Menu #
''' Menu0 '''
Action1 = QtGui.QAction('LOAD SGV', self)
Action1.setStatusTip('Action 1')
# Action1.triggered.connect(self.F_Action1)

Action2 = QtGui.QAction('SAVE SGV', self)
Action2.setStatusTip('Action 2')
# Action2.triggered.connect(self.F_Action2)

Action3 = QtGui.QAction('EXPORT TO MOBILE', self)
Action3.setStatusTip('Action 3')
# Action3.triggered.connect(self.F_Action3)

menubar = self.menuBar()

ChMenu = menubar.addMenu('&Action')
ChMenu.addAction(Action1)
ChMenu.addAction(Action2)
ChMenu.addAction(Action3)

'''Menu2'''
PopFen1 = QtGui.QAction('PopUp', self)
PopFen1.setStatusTip('PopFen1')
PopFen1.triggered.connect(self.F_Popup)

menubar = self.menuBar()

PopMenu = menubar.addMenu('&PopFen')
PopMenu.addAction(PopFen1)

### ----------------------------------------------- ###
''' Dock '''
self.ComboBox_0 = QtGui.QComboBox()
self.ComboBox_0.setGeometry(QtCore.QRect(10,10,10, 10))
self.ComboBox_0.addItem("HANDSET")
self.ComboBox_0.addItem("HANDFREE")
self.ComboBox_0.addItem("CAR KIT")
self.ComboBox_0.addItem("RSM")

self.ComboBox_1 = QtGui.QComboBox()
self.ComboBox_1.setGeometry(QtCore.QRect(10,10,10, 10))
self.ComboBox_1.addItem("VOICE")
self.ComboBox_1.addItem("RING")
self.ComboBox_1.addItem("BEEP")

VBoxLayout_1 = QtGui.QVBoxLayout()
VBoxLayout_1.addWidget(self.ComboBox_0)
VBoxLayout_1.addWidget(self.ComboBox_1)
VBoxLayout_1.setSpacing(200)

Widget_1 = QtGui.QWidget()
Widget_1.setLayout(VBoxLayout_1)

F_Dock_Wid_Vitesse = QtGui.QDockWidget()
F_Dock_Wid_Vitesse.setWidget(Widget_1)
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, F_Dock_Wid_Vitesse)
### ----------------------------------------------- ###

self.SpinBox_1 = QtGui.QSpinBox(self)
self.SpinBox_1.setRange(-12,12)
self.SpinBox_1.setSingleStep(1)
self.SpinBox_1.setValue(0)

self.SpinBox_2 = QtGui.QSpinBox(self)
self.SpinBox_2.setRange(-12,12)
self.SpinBox_2.setSingleStep(1)
self.SpinBox_2.setValue(0)

self.SpinBox_3 = QtGui.QSpinBox(self)
self.SpinBox_3.setRange(-12,12)
self.SpinBox_3.setSingleStep(1)
self.SpinBox_3.setValue(0)

self.SpinBox_4 = QtGui.QSpinBox(self)
self.SpinBox_4.setRange(-12,12)
self.SpinBox_4.setSingleStep(1)
self.SpinBox_4.setValue(0)

self.SpinBox_5 = QtGui.QSpinBox(self)
self.SpinBox_5.setRange(-12,12)
self.SpinBox_5.setSingleStep(1)
self.SpinBox_5.setValue(0)

self.SpinBox_6 = QtGui.QSpinBox(self)
self.SpinBox_6.setRange(-12,12)
self.SpinBox_6.setSingleStep(1)
self.SpinBox_6.setValue(0)

GridLayout_1 = QtGui.QGridLayout()
GridLayout_1.addWidget(self.SpinBox_1, 0,0,1,1)
GridLayout_1.addWidget(self.SpinBox_2, 0,1,1,1)
GridLayout_1.addWidget(self.SpinBox_3, 0,2,1,1)
GridLayout_1.addWidget(self.SpinBox_4, 1,0,1,1)
GridLayout_1.addWidget(self.SpinBox_5, 1,1,1,1)
GridLayout_1.addWidget(self.SpinBox_6, 1,2,1,1)

O_GroupBox_1 = QtGui.QGroupBox()
O_GroupBox_1.setLayout(GridLayout_1)

self.setCentralWidget(O_GroupBox_1)

### ----------------------------------------------- ###
def F_Popup(self):
self.w = MyPopup()
self.w.setGeometry(QtCore.QRect(100, 100, 400, 200))
self.w.show()
### ----------------------------------------------- ###

def main():
'''Application'''
App = QtGui.QApplication(sys.argv)
App.setStyle("windows")
MainWin2 = essai()
MainWin2.show()
sys.exit(App.exec_() )

if __name__=='__main__':
main()



Thank you everybody.

ChrisW67
14th April 2014, 22:13
QFile, QTextStream, a while() loop, and calls to QSpinBox::setValue()