User:Axiom: Difference between revisions
No edit summary |
No edit summary |
||
Line 105: | Line 105: | ||
</code> | </code> | ||
You will also need the [[User:Axiom/readinput.ui|ui file]] that goes along with that. | You will also need the [[User:Axiom/readinput.ui|ui file]] that goes along with that. | ||
==Writing input to a file, and bringing it back== | |||
This snippet does the same as above, except it saves whatever you type as config.txt, and the next time you run it, it loads the text back into the input field. I got this code from [http://www.daniweb.com/code/snippet389.html here], and [http://www.devshed.com/c/a/Python/PyQT-Input-Widgets/ here]. | |||
===readinput.py=== | |||
<code python> | |||
import sys | |||
#Load the widget | |||
from PyQt4.QtGui import QWidget #This is a widget, not a window, and that makes a difference | |||
from PyQt4.uic import loadUiType #We need this to dynamically load the .ui file we created in qt designer | |||
from PyQt4.QtCore import pyqtSignature, Qt #Not sure what this signature thing is, but apparently it is great for buttons (signals/slots?) | |||
import sys | |||
(form, formbase) = loadUiType('readinput.ui') #this is the .ui file we want to show | |||
class readInputWidget(QWidget, form): #in this class, we load the window, and assign actions to the buttons | |||
def __init__(self, parent=None): | |||
QWidget.__init__(self, parent) | |||
self.setAttribute(Qt.WA_DeleteOnClose) #This doesn't seem to change anything if you delete it, but may be usefull for housekeeping | |||
self.setupUi(self) | |||
# read back the entire test string as a string with read() | |||
filename = "config.txt" | |||
try: | |||
fin = open(filename, "r") | |||
str3 = fin.read() | |||
fin.close() | |||
print "Contents of file %s:" % filename | |||
print str3 | |||
self.inputBox1.setText(str3) #This is the line where we set the input box to display whatever was written to the config file last time | |||
except IOError: | |||
print "File %s does not exist!" % filename | |||
@pyqtSignature("on_exitButton_clicked()") | |||
def on_exitButton_clicked(self): | |||
self.close() | |||
@pyqtSignature("on_evalButton_clicked()") | |||
def on_evalButton_clicked(self): | |||
print 'This should echo our text:' | |||
userinput = self.inputBox1.text() #This is the fantastic line of code we were waiting for! Make sure you have an input box by that name, and specify that it is text | |||
print userinput | |||
# a look at file handling in Python | |||
# tested with Python24 vegaseat 29sep2005 | |||
# set up a test string | |||
str1 =userinput | |||
# let's create our test file by writing the test string to the working folder/directory with write() | |||
# modifier "w" is for writing text, use "wb" for binary data like images | |||
fout = open("config.txt", "w") | |||
fout.write(str1) | |||
fout.close() | |||
def main(): | |||
from PyQt4.QtGui import QApplication | |||
from PyQt4.QtCore import Qt | |||
app = QApplication(sys.argv) | |||
widget = readInputWidget() | |||
widget.show() | |||
return app.exec_() | |||
if __name__ == "__main__": | |||
sys.exit(main()) | |||
</code> | |||
This uses the exact same [[User:Axiom/readinput.ui|ui file]] as above. |
Revision as of 20:15, 28 March 2007
Axiom's sandbox
I am just putting some stuff here right now, because you have such nice color coding in your wiki. Maybe I'll turn it into a tutorial if I figure out what I am doing.
Dynamically Loading a .ui file
I found this snippet here (french). I don't know if this is the right way to do it, but it seems to work.
loadui.py
import sys
from PyQt4 import QtGui, uic
app = QtGui.QApplication(sys.argv)
widget = uic.loadUi("demo.ui")
widget.show()
- La boucle principale
app.exec_()
Of course you will need to make a ui file called demo.ui for this to work.
Connecting Buttons From Dynamically loaded .ui File
This code really works.
callMainWindow.py
import sys
from PyQt4 import QtGui
from PyQt4 import uic
from PyQt4 import QtCore
- the class has to have the same base class as your UI
class DemoImpl(QtGui.QMainWindow):
def __init__(self, *args):
QtGui.QMainWindow.__init__(self, *args)
# just pass self
uic.loadUi("mainWindow.ui", self)
#this line makes sure that we are only looking for a string
#if we want a bool as well we can add @QtCore.pyqtSignature("bool")
@QtCore.pyqtSignature("")
def on_button1_clicked(self):
print "You Clicked the button!"
@QtCore.pyqtSignature("")
def on_button2_clicked(self):
print "You Clicked the button2!"
app = QtGui.QApplication(sys.argv)
widget = DemoImpl()
widget.show()
app.exec_()
You will need this file too.
Getting user input into a variable
Once you have it in a variable, you can throw the whole power of python at it. I have tried to show how to do this in the simplest way possible. Note: This code shamelessly stolen from xpathevaluator. Lots of good pyqt4 sample code in there. Also might be helpful if you want to evaluate an xpath. But I'm not sure why you would want to do that.
readinput.py
import sys
#Load the widget
from PyQt4.QtGui import QWidget #This is a widget, not a window, and that makes a difference
from PyQt4.uic import loadUiType #We need this to dynamically load the .ui file we created in qt designer
from PyQt4.QtCore import pyqtSignature, Qt #Not sure what this signature thing is, but apparently it is great for buttons (signals/slots?)
import sys
(form, formbase) = loadUiType('readinput.ui') #this is the .ui file we want to show
class readInputWidget(QWidget, form): #in this class, we load the window, and assign actions to the buttons
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setAttribute(Qt.WA_DeleteOnClose) #This doesn't seem to change anything if you delete it, but may be usefull for housekeeping
self.setupUi(self)
@pyqtSignature("on_exitButton_clicked()")
def on_exitButton_clicked(self):
self.close()
@pyqtSignature("on_evalButton_clicked()")
def on_evalButton_clicked(self):
print 'This should echo our text:'
userinput = self.inputBox1.text() #This is the fantastic line of code we were waiting for! Make sure you have an input box by that name, and specify that it is text
print userinput
def main():
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import Qt
app = QApplication(sys.argv)
widget = readInputWidget()
widget.show()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
You will also need the ui file that goes along with that.
Writing input to a file, and bringing it back
This snippet does the same as above, except it saves whatever you type as config.txt, and the next time you run it, it loads the text back into the input field. I got this code from here, and here.
readinput.py
import sys
#Load the widget
from PyQt4.QtGui import QWidget #This is a widget, not a window, and that makes a difference
from PyQt4.uic import loadUiType #We need this to dynamically load the .ui file we created in qt designer
from PyQt4.QtCore import pyqtSignature, Qt #Not sure what this signature thing is, but apparently it is great for buttons (signals/slots?)
import sys
(form, formbase) = loadUiType('readinput.ui') #this is the .ui file we want to show
class readInputWidget(QWidget, form): #in this class, we load the window, and assign actions to the buttons
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setAttribute(Qt.WA_DeleteOnClose) #This doesn't seem to change anything if you delete it, but may be usefull for housekeeping
self.setupUi(self)
# read back the entire test string as a string with read()
filename = "config.txt"
try:
fin = open(filename, "r")
str3 = fin.read()
fin.close()
print "Contents of file %s:" % filename
print str3
self.inputBox1.setText(str3) #This is the line where we set the input box to display whatever was written to the config file last time
except IOError:
print "File %s does not exist!" % filename
@pyqtSignature("on_exitButton_clicked()")
def on_exitButton_clicked(self):
self.close()
@pyqtSignature("on_evalButton_clicked()")
def on_evalButton_clicked(self):
print 'This should echo our text:'
userinput = self.inputBox1.text() #This is the fantastic line of code we were waiting for! Make sure you have an input box by that name, and specify that it is text
print userinput
# a look at file handling in Python
# tested with Python24 vegaseat 29sep2005
# set up a test string
str1 =userinput
# let's create our test file by writing the test string to the working folder/directory with write()
# modifier "w" is for writing text, use "wb" for binary data like images
fout = open("config.txt", "w")
fout.write(str1)
fout.close()
def main():
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import Qt
app = QApplication(sys.argv)
widget = readInputWidget()
widget.show()
return app.exec_()
if __name__ == "__main__":
sys.exit(main())
This uses the exact same ui file as above.