Python Forum
How to program PyQt4 from python? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: How to program PyQt4 from python? (/thread-2185.html)



How to program PyQt4 from python? - medic_ward - Feb-24-2017

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..!!!!


RE: How to program PyQt4 from python? - Axel_Erfurt - Feb-24-2017

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_())



RE: How to program PyQt4 from python? - iFunKtion - Feb-25-2017

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/10/getting-started.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.


RE: How to program PyQt4 from python? - medic_ward - Feb-27-2017

Thank you both, I am so new to python and PyQt4 that things are coming along slowly. I appreciate the help and the link.