Python Forum
How to program PyQt4 from python?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to program PyQt4 from python?
#1
I have this code to run in python.

# This program searches through Exchange logs

# to find the source of account lockouts
# ONLY WORKS IN PYTHON 3


# Imports Regular Expressions and GLOB
import re, glob

def findusername():
       print()
       return

# Prompts user for a username
raw_user = raw_input("Type the username:")
user = raw_user

print("Please wait while the program searches for "+user)
print("The program could take up to two minutes to complete")

# Creates a list to store the lines of the log files
clean_log = []

# Creates a list of each log file path
logs = glob.glob("\\\xxxxxxxxxxx\c$\inetpub\logs\LogFiles\W3SVC1\*.log")

# Cycles through each individual log file
for log in logs:
       # Opens the file as "searchfile" variable
       with open(log, "r") as searchfile:
               # Looks at each line in "searchfile"
               for line in searchfile:
                       # Locates the "user" variable in the text file
                       if re.search(user, line, re.M|re.I):
                               # Locates 401 (access denied) errors
                               if re.search(r'\s401\s', line, re.M|re.I):
                                       clean_log.append(line)

                                                   
# Prints each line of the parsed log list
for line in clean_log:
       print(line)

raw_input("Press enter to exit")
and it works great but trying to program in a gui and im getting lost trying to get it to work. So i set up a gui to test with and exported it into a py file and got this.
# -*- coding: utf-8 -*-


# Form implementation generated from reading ui file 'austin.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!



from PyQt4 import QtCore, QtGui

try:
   _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
   def _fromUtf8(s):
       return s

try:
   _encoding = QtGui.QApplication.UnicodeUTF8
   def _translate(context, text, disambig):
       return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
   def _translate(context, text, disambig):
       return QtGui.QApplication.translate(context, text, disambig)

class Ui_ExchangeSearch(object):




   def setupUi(self, ExchangeSearch):
       ExchangeSearch.setObjectName(_fromUtf8("ExchangeSearch"))
       ExchangeSearch.resize(800, 391)
       self.centralwidget = QtGui.QWidget(ExchangeSearch)
       self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
       self.Run = QtGui.QPushButton(self.centralwidget)
       self.Run.setGeometry(QtCore.QRect(430, 30, 75, 23))
       self.Run.setObjectName(_fromUtf8("Run"))
       self.NameLabel = QtGui.QLabel(self.centralwidget)
       self.NameLabel.setGeometry(QtCore.QRect(30, 30, 131, 21))
       self.NameLabel.setObjectName(_fromUtf8("NameLabel"))

########################## close form ###########################
       self.Closebtn = QtGui.QPushButton(self.centralwidget)
       self.Closebtn.setGeometry(QtCore.QRect(700, 320, 75, 23))
       self.Closebtn.setObjectName(_fromUtf8("Close"))
       self.Closebtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
#################################################################

       self.NameInput = QtGui.QLineEdit(self.centralwidget)
       self.NameInput.setGeometry(QtCore.QRect(180, 30, 231, 20))
       self.NameInput.setObjectName(_fromUtf8("NameInput"))
       self.SearchResult = QtGui.QTextBrowser(self.centralwidget)
       self.SearchResult.setGeometry(QtCore.QRect(20, 110, 751, 192))
       self.SearchResult.setObjectName(_fromUtf8("SearchResult"))
       ExchangeSearch.setCentralWidget(self.centralwidget)
       self.menubar = QtGui.QMenuBar(ExchangeSearch)
       self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
       self.menubar.setObjectName(_fromUtf8("menubar"))
       self.menuFile = QtGui.QMenu(self.menubar)
       self.menuFile.setObjectName(_fromUtf8("menuFile"))
       ExchangeSearch.setMenuBar(self.menubar)
       self.statusbar = QtGui.QStatusBar(ExchangeSearch)
       self.statusbar.setObjectName(_fromUtf8("statusbar"))
       ExchangeSearch.setStatusBar(self.statusbar)

################## close form through menubar ###################
       self.actionExit = QtGui.QAction(ExchangeSearch)
       self.actionExit.setObjectName(_fromUtf8("actionExit"))
       self.actionExit.triggered.connect(QtCore.QCoreApplication.instance().quit) 
       ################################################################# 

       self.menuFile.addAction(self.actionExit)
       self.menubar.addAction(self.menuFile.menuAction())

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





   def retranslateUi(self, ExchangeSearch):
       ExchangeSearch.setWindowTitle(_translate("ExchangeSearch", "Exchange Search Utility", None))
       self.Run.setText(_translate("ExchangeSearch", "Run", None))
       self.NameLabel.setText(_translate("ExchangeSearch", "Please Enter User Name:", None))
       self.Closebtn.setText(_translate("ExchangeSearch", "Close", None))
       self.menuFile.setTitle(_translate("ExchangeSearch", "File", None))
       self.actionExit.setText(_translate("ExchangeSearch", "Exit", None))


if __name__ == "__main__":
   import sys
   app = QtGui.QApplication(sys.argv)
   ExchangeSearch = QtGui.QMainWindow()
   ui = Ui_ExchangeSearch()
   ui.setupUi(ExchangeSearch)
   ExchangeSearch.show()
   sys.exit(app.exec_())
When I try to code the run button to perform it never works or displays anything in text box. So i delete all the code I added and start over.. Any hints or clues to help me...?

Thanks in advance...

A real python beginner..!!!!
Reply
#2
if you want text from Name Label to Text Box

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


from PyQt4 import QtCore, QtGui
 
try:
   _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
   def _fromUtf8(s):
       return s
 
try:
   _encoding = QtGui.QApplication.UnicodeUTF8
   def _translate(context, text, disambig):
       return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
   def _translate(context, text, disambig):
       return QtGui.QApplication.translate(context, text, disambig)
 
class Ui_ExchangeSearch(object):

   def setupUi(self, ExchangeSearch):
       ExchangeSearch.setObjectName(_fromUtf8("ExchangeSearch"))
       ExchangeSearch.resize(800, 391)
       self.centralwidget = QtGui.QWidget(ExchangeSearch)
       self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
       self.Run = QtGui.QPushButton(self.centralwidget)
       self.Run.setGeometry(QtCore.QRect(430, 30, 75, 23))
       self.Run.setObjectName(_fromUtf8("Run"))
       self.Run.clicked.connect(self.setName)
       self.NameLabel = QtGui.QLabel(self.centralwidget)
       self.NameLabel.setGeometry(QtCore.QRect(30, 30, 150, 21))
       self.NameLabel.setObjectName(_fromUtf8("NameLabel"))
 
########################## close form ###########################
       self.Closebtn = QtGui.QPushButton(self.centralwidget)
       self.Closebtn.setGeometry(QtCore.QRect(700, 320, 75, 23))
       self.Closebtn.setObjectName(_fromUtf8("Close"))
       self.Closebtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
#################################################################
 
       self.NameInput = QtGui.QLineEdit(self.centralwidget)
       self.NameInput.setGeometry(QtCore.QRect(180, 30, 231, 20))
       self.NameInput.setObjectName(_fromUtf8("NameInput"))
       self.SearchResult = QtGui.QTextBrowser(self.centralwidget)
       self.SearchResult.setGeometry(QtCore.QRect(20, 110, 751, 192))
       self.SearchResult.setObjectName(_fromUtf8("SearchResult"))
       ExchangeSearch.setCentralWidget(self.centralwidget)
       self.menubar = QtGui.QMenuBar(ExchangeSearch)
       self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
       self.menubar.setObjectName(_fromUtf8("menubar"))
       self.menuFile = QtGui.QMenu(self.menubar)
       self.menuFile.setObjectName(_fromUtf8("menuFile"))
       ExchangeSearch.setMenuBar(self.menubar)
       self.statusbar = QtGui.QStatusBar(ExchangeSearch)
       self.statusbar.setObjectName(_fromUtf8("statusbar"))
       ExchangeSearch.setStatusBar(self.statusbar)
 
################## close form through menubar ###################
       self.actionExit = QtGui.QAction(ExchangeSearch)
       self.actionExit.setObjectName(_fromUtf8("actionExit"))
       self.actionExit.triggered.connect(QtCore.QCoreApplication.instance().quit) 
       ################################################################# 
 
       self.menuFile.addAction(self.actionExit)
       self.menubar.addAction(self.menuFile.menuAction())
 
       self.retranslateUi(ExchangeSearch)
       QtCore.QMetaObject.connectSlotsByName(ExchangeSearch)

   def retranslateUi(self, ExchangeSearch):
       ExchangeSearch.setWindowTitle(_translate("ExchangeSearch", "Exchange Search Utility", None))
       self.Run.setText(_translate("ExchangeSearch", "Run", None))
       self.NameLabel.setText(_translate("ExchangeSearch", "Please Enter User Name:", None))
       
       self.Closebtn.setText(_translate("ExchangeSearch", "Close", None))
       self.menuFile.setTitle(_translate("ExchangeSearch", "File", None))
       self.actionExit.setText(_translate("ExchangeSearch", "Exit", None))

   def setName(self):
       self.SearchResult.setText(self.NameInput.text())
 
if __name__ == "__main__":
   import sys
   app = QtGui.QApplication(sys.argv)
   ExchangeSearch = QtGui.QMainWindow()
   ui = Ui_ExchangeSearch()
   ui.setupUi(ExchangeSearch)
   ExchangeSearch.show()
   sys.exit(app.exec_())
Reply
#3
Files generated from Qt designer are not designed to be modified. They are used as ui modules and as such stay as generated. I found this blog, which goes through all of this and makes much more sense: https://bitesofcode.blogspot.co.uk/2011/...arted.html.

Once you get to grips with how this fellow works with PyQt4 life gets a little easier. A bonus to this is all his code is correct and works.
Reply
#4
Thank you both, I am so new to python and PyQt4 that things are coming along slowly. I appreciate the help and the link.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt4 dynamic QComboBox littleGreenDude 4 5,671 Jan-02-2019, 07:22 PM
Last Post: littleGreenDude
  [PyQt] PyQt4 handle dynamic checkbox click littleGreenDude 1 6,578 Dec-27-2018, 09:17 PM
Last Post: littleGreenDude
  PyQt4 installation frustration littleGreenDude 4 4,538 Dec-27-2018, 04:29 PM
Last Post: littleGreenDude
  [PyQt4] Is it right python coding scheme between TCP Server Thread and GUI class ? KimTom 3 3,245 Sep-18-2018, 01:21 PM
Last Post: Alfalfa
  How to Integrate PyQt4 Custom Widget with Qt Designer Zukias 1 3,911 Aug-29-2018, 05:33 PM
Last Post: Zukias
  Updating Python version from command prompt and Conversion from PyQt4 to PyQt5 Vysero 4 4,950 Jul-19-2018, 03:15 PM
Last Post: Vysero
  Trouble displaying an image in PyQt4 Vysero 2 3,092 Jul-01-2018, 05:25 PM
Last Post: Alfalfa
  [PyQt] problem with PyQt4 jss15497 0 2,760 Jan-16-2018, 08:32 AM
Last Post: jss15497
  [PyQt] Learn pyqt5 using pyqt4 jimclay75051 5 5,765 Nov-13-2017, 06:29 AM
Last Post: zykbee
  PyQT4 only upper case text iFunKtion 1 3,672 Feb-27-2017, 01:13 PM
Last Post: Raures

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020