D
D
Dmitry Bereza2017-10-28 05:13:50
Python
Dmitry Bereza, 2017-10-28 05:13:50

How to attach a GUI to a Python script?

Greetings. I have a python script that assigns a hid to a device (in my case a usb button) a key from the keyboard. There is also a GUI that I made in pyqt4. How can I put it all together now? So that when I click Start, the value entered in the window is assigned to the variable ptt_key if the device is found Connected and if not found disconnected? Tell. I'm not very good at programming myself. Thank you.
The script itself

import time
import pywinusb.hid as hid
from win32api import keybd_event

class App:

    hid_vendor_id = 0x07CA
    hid_product_id = 0x9850
    hid_device_list = None
    sound_enabled = True

    def __init__(self, ptt_key=0x08):
        self._ptt_key = ptt_key
 
    def set_ptt_key(self, value):
        self._ptt_key = value
 
    def get_ptt_key(self):
        return self._ptt_key
    ptt_key = property(get_ptt_key, set_ptt_key)

    def start( self ):
        self.hid_device_list = hid.HidDeviceFilter( vendor_id = self.hid_vendor_id, product_id = self.hid_product_id )
        self.hid_device_list = self.hid_device_list.get_devices( )

        print "Looking for Device... \nPress Control+C to Quit"

        if self.hid_device_list:
            for device in self.hid_device_list:
                    print "Found One!\n"
                    device.open( )
                    device.set_raw_data_handler( self.raw_input_callback )
        else:
            print "Oh No, no devices were found! \n"

        while True:
            time.sleep( 1000 ) #HID input/keysend on separate thread.

    def raw_input_callback( self, data ):

        if data[2] == 1:
            keybd_event( self.ptt_key, 0, 0x0000, 0 )
        elif data[2] == 0:
            keybd_event( self.ptt_key, 0, 0x0002, 0 )

if __name__ == '__main__':
    app = App( )
    app.start( )

GUI
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'HotKey.ui'
#
# Created: Sat Oct 28 05:12:26 2017
#      by: PyQt4 UI code generator 4.9.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(268, 70)
        self.pbStart = QtGui.QPushButton(Dialog)
        self.pbStart.setGeometry(QtCore.QRect(20, 40, 75, 23))
        self.pbStart.setObjectName(_fromUtf8("pbStart"))
        self.leButtonCode = QtGui.QLineEdit(Dialog)
        self.leButtonCode.setGeometry(QtCore.QRect(120, 10, 61, 20))
        self.leButtonCode.setReadOnly(True)
        self.leButtonCode.setObjectName(_fromUtf8("leButtonCode"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20, 10, 91, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.pbStop = QtGui.QPushButton(Dialog)
        self.pbStop.setGeometry(QtCore.QRect(110, 40, 75, 23))
        self.pbStop.setObjectName(_fromUtf8("pbStop"))
        self.lStatus = QtGui.QLabel(Dialog)
        self.lStatus.setGeometry(QtCore.QRect(190, 10, 71, 21))
        self.lStatus.setObjectName(_fromUtf8("lStatus"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Hot Key", None, QtGui.QApplication.UnicodeUTF8))
        self.pbStart.setText(QtGui.QApplication.translate("Dialog", "Start", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Dialog", "Button code", None, QtGui.QApplication.UnicodeUTF8))
        self.pbStop.setText(QtGui.QApplication.translate("Dialog", "Stop", None, QtGui.QApplication.UnicodeUTF8))
        self.lStatus.setText(QtGui.QApplication.translate("Dialog", "<font color=\"red\">disconnected</font>", None, QtGui.QApplication.UnicodeUTF8))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2017-10-28
@tsarevfs

See examples here www.rkblog.rk.edu.pl/w/p/simple-text-editor-pyqt4 The
first two links are enough for you.

# here we connect signals with our slots
    QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"), self.file_dialog)
.
Thanks to this line in the example, when the button is clicked, the file_dialog function is called.

S
Sergey6661313, 2017-11-10
@Sergey6661313

In general, in short,
you need to create an object of the QApplication class
and at the end of the code, instead of app.start, execute QApplication.exec([])
of course, you will also have to create a qwidget and assign setupUi from Ui_Dialog to it
and, of course, you can bind a button to your start command
like this:
pbStart.clicked.connect(app.start)
or as comrade tsarevfs wrote , ptt_key
values ​​are assigned like this:
hex_str = uidialog.leButtonCode.text() # get the text from the window
hex_int = int(hex_str, 16) # turn it into a number
hex_key = hex( hex_int) # back to hex
self.set_ptt_key(hex_int) # And... get signed.
I wrote a little code, you can look at github

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question