This is my mainscript which calls some generated UI and defines a few functions:
import uifile
from PyQt5 import QtWidgets
import sys
class App(QtWidgets.QMainWindow, uifile.Ui_MyWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.btn_clickMe.clicked.connect(self.some_function)
return
def some_function(self):
import otherpyscript
def input_user(self):
user = self.lineEdit_username.text()
return user
def main():
app = QtWidgets.QApplication(sys.argv)
form = App()
form.show()
app.exec_()
if __name__ == '__main__':
main()
This is the other script where I call the function from mainscript:
...
import mainscript
print("The user input from your other script is: " + mainscript.App().input_user())
I'm trying to get mainscript.App().input_user() to not show up as empty (the default for PyQt).
EDIT:
On second thoughts, the right approach depends on how mainscript is used. If it is used as the start-up script, it will not be initially imported, which then complicates things when it comes to accessing its globals later on.
A better approach is to have a very simple start-up script that only has this in it:
# mainscript
if __name__ == '__main__':
from mainmodule import main
main()
The mainmodule would then look like this:
# mainmodule
import sys
from PyQt5 import QtWidgets
import uifile
class App(QtWidgets.QMainWindow, uifile.Ui_MyWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.btn_clickMe.clicked.connect(self.some_function)
def some_function(self):
import othermodule
othermodule.print_user()
def input_user(self):
user = self.lineEdit_username.text()
return user
form = None
def main_window():
return form
def main():
global form
app = QtWidgets.QApplication(sys.argv)
form = App()
form.show()
app.exec_()
And othermodule would look like this:
# othermodule
import mainmodule
def print_user():
print("user input: " + mainmodule.main_window().input_user())
Related
I spent all day debugging and stripping my project to fix this issue.
I have a help window(Help_ui class) which should open if the text.txt is not found and the main window(Main class)which has a QTextEdit box which should print "Hello".
The problem is that when I call the Main window from the Help_ui class it doesn't print "Hello", but it does if I call it from the logic.
Why is doing this, does it open another instance of that windows and not the proper window?
from PyQt5 import QtWidgets
import sys
from pathlib import Path
from ui_files import mainWindow
from ui_files import help_ui
text = "Hello"
class Main(QtWidgets.QMainWindow, mainWindow.Ui_mainWindow):
def __init__(self, text):
super().__init__()
self.setupUi(self)
self.mainTextEdit.setText(text)
class Help_ui(QtWidgets.QDialog, help_ui.Ui_help):
def __init__(self):
super().__init__()
self.setupUi(self)
self.close_btn.clicked.connect(self.close)
self.form = Main(None)
self.form.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
form = Main(text)
my_file = Path("file.txt")
if my_file.is_file():
form.show()
else:
help_window = Help_ui()
help_window.show()
app.exec_()
What happens is that the instance form is different from the instance self.form, in the first you pass as a parameter text, instead in the second None.
Must be change self.form = Main(None) to self.form = Main(text)
I am trying to return a path from a browse folder dialog box.
I have tried passing the instance object or even attribute to the call and setting it there:
self.browseBtn.clicked.connect(myExporter.browseFolder(self))
or
self.browseBtn.clicked.connect(myExporter.browseFolder(self.path))
But this doesn't work. It causes the browser dialog to pop open immediately upon load and then once you choose a folder it errors out with : Failed to connect signal clicked().
I have tried to set the clicked call to a return, with no luck:
result = self.browseBtn.clicked.connect(myExporter.browseFolder)
Can someone lead me in the right direction as far as how to return a value, when you are dealing with separate classes handling the UI and logic? Also... is it bad practice to be separating them like this? I know I could probably easily solve this if I threw everything into just one python file, but I figured that is not proper.
Here is my ui file (ui.py):
from PySide import QtCore, QtGui
class Ui_Dialog(object):
def __init__(self):
self.path =""
def setupUi(self, Dialog, myExporter):
Dialog.setObjectName("Dialog")
Dialog.resize(382, 589)
...
.......
.............
.................
self.retranslateUi(Dialog)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.browseBtn.clicked.connect(myExporter.browseFolder)
Here is my exporter file (exporter.py):
class Exporter(object):
def __init__(self):
...
......
def browseFolder(self):
...
.......
do something
...........
return path
Here is my load/test file (loadExporter.py):
import ui as interface
import exporter as exporter
from PySide import QtCore, QtGui
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
myExporter = exporter.Exporter()
myUI = interface.Ui_Dialog()
myUI.setupUi(Dialog, myExporter)
Dialog.show()
app.exec_()
It's not necessarily bad to have them in separate files. Having a separate file for certain widgets is a good thing especially if those widgets can be reused.
I would have my main file have a QMainWindow class.
class MyWindow(QtGui.QMainWindow):
pass
if __name__ == "__main__":
QtGui.QApplication([])
mywindow = MyWindow()
mywindow.show()
sys.exit(QtGui.qApp.exec_())
Wrapping the app functionality in if __name__ == "__main__" prevents this code from being run when another file tries to import this file.
A signal (self.browserBtn.clicked) calls a callback method. Everything is an object in python.
def my_func():
pass
new_func_name = my_func # my_func can be reassigned like any variable
my_func is an object. self.browseBtn.clicked.connect(my_func) passes my_func as a variable to be called later.
When self.browserBtn.clicked.emit() happens (on user click) it is the same as calling the connected functions my_func(). Other signals may pass values to callback functions self.lineEdit.textChanged.connect(my_func) calls 'my_func(new_text)'
You want your function to call everything for you.
def open_file(filename=None):
"""Open a file."""
# If a filename isn't given ask the user for a file
if filename is None:
filename, ext = QtGUi.QFileDialog.getOpenFileName(None, "Open File", ".../My Documents/")
# You may have to use the ext to make a proper filename
# Open the file
with open(filename, "r") as file:
file.read()
self.browserBtn.clicked.connect(open_file)
Structure
...
import mywidget
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super().__init__()
...
self.show_file = QtGui.QLineEdit()
self.setCentralWidget(self.show_file)
self.exporter = mywidget.Exporter()
self.browserBtn.clicked.connect(self.open_file)
def open_file(self, filename=None):
"""Open a file."""
path = self.exporter.browseFolder()
# Do something with the path
self.show_file.setText(path)
if __name__ == "__main__":
QtGui.QApplication([])
mywindow = MyWindow()
mywindow.show()
sys.exit(QtGui.qApp.exec_())
I don't think I fully understand what you're trying to achieve, but may I suggest the following solution.
exporter.py
# implementation
dialog.py (Main UI)
import PyQt4.QtGui as qg
from exporter import Exporter
class Dialog(qg.QDialog):
def __init__(self):
super().__init__()
self.path = None
self.setup_widgets()
def setup_widgets(self):
self.browse.clicked.connect(self.on_browse_clicked)
def on_browse_clicked(self):
self.path = Exporter().browse_folder()
main.py
import sys
import PyQt4.QtGui as qg
from dialog import Dialog
if __name__ == '__main__':
app = qg.QApplication(sys.argv)
dialog = Dialog()
dialog.show()
sys.exit(app.exec_())
This way you still have three files, but dialog.py imports exporter.py instead of main.py importing both of them.
As for returning values from signals, I don't know; I think signals are voids (do not return values).
Signals pass arguments to slots via their formal parameters. Usually slots have the same or fewer parameters than their signal counterparts'. It's slots that return values.
I'm trying to make unit tests on classes that involve signals and QTimers. (see the answer to this question for an idealization of one of the classes). I'm having a hard time trying to understand where to put the app.exec_() call without freezing everything and executing the tests with the signals being emitted. Just like in the question I cite, I think the problem here is that I need an event loop but putting in the setUpClass definitely doesn't work. This is the smallest piece of code I made to reproduce my problem, which in reality is with a larger code base.
testSignals.py:
import time
from PySide import QtCore
class TestSignals(QtCore.QObject):
def __init__(self):
self.timer = QtCore.QTimer()
self.myTime = ''
self.timer.timeout.connect(self.updateTime)
self.timer.start(100)
def getTime(self):
return self.myTime
def updateTime(self):
self.myTime = time.strftime('%H:%M:%S')
def stop(self):
self.timer.stop()
unitTesting.py:
import unittest
from testSignals import TestSignals
from PySide import QtGui, QtCore
from testSignals import TestSignals
import sys
class test(unittest.TestCase):
#classmethod
def setUpClass(cls):
app = QtGui.QApplication(sys.argv)
assert id(app) == id(QtGui.qApp) and id(app) == id(QtCore.QCoreApplication.instance())
cls.test = TestSignals()
# app.exec_() # uncommenting this only causes the code to freeze
def testStarted(self):
mytime = self.test.getTime()
self.assertNotEqual(mytime,'','Time is still empty')
if __name__ == '__main__':
unittest.main()
and the result is a failure of the only test because "mytime" is still empty. Just for clarity, the class does work:
class testConnection():
def receiveMe(self):
print(self.test.getTime())
# print(self.test.getData())
# signal is emmited when data is changed
def __init__(self):
self.test = TestSignals()
self.test.timer.timeout.connect(self.receiveMe)
# self.test.start()
app = QtCore.QCoreApplication([])
test = testConnection()
timer = QtCore.QTimer()
timer.singleShot(4000,app.exit)
app.exec_()
produces:
>>>$ python3 testConection.py
14:50:21
14:50:22
14:50:23
14:50:24
I try to get the value of Count_total_value in my user interface when I call Continusread method, (it should be "toto" according to what I want to do) but it is always the default value "azerty" which is displayed. Please can you tell me where I am wrong?
This is my code:
#!/usr/bin/env python3
from PyQt4 import QtCore, QtGui
import sys
import os
import subprocess
import time
import threading
from ctypes import *
import ctypes
#import Converted Python UI File
from test_error_rx import Ui_MainWindow
class MyThread(QtCore.QThread):
Count_total_valuechanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent=parent)
self.Count_total_value = 'Azerty'
def run(self):
##do things to calculate Count_total_value
Count_total_value='toto'
self.Count_total_valuechanged.emit((self.Count_total_value))
time.sleep(0.1)
class Main( QtGui.QMainWindow,QtGui.QWidget):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connect the Buttons
QtCore.QObject.connect(self.ui.Continusread,QtCore.SIGNAL("clicked()"),self.Continusread)
self.__thread = MyThread(self)
self.__thread.Count_total_valuechanged.connect(self.ui.Count_total.setText)
def Continusread(self):
self.__thread.start()
def main():
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
In the run() method of your thread class MyThread you set Count_total_value='toto' when it should be self.Count_total_value='toto'.
Note that when posting on stackoverflow you should:
post a minimilistic working example (you haven't included your UI in the above code so no-one can run your script)
Check the posted code has the correct indentation and fix any mistakes (your posted code is a mess of incorrect indentation)
Why doesn't the following example work?
from PyQt4 import QtGui
import sys
class TestView(QtGui.QWidget):
def __init__(self):
super(TestView, self).__init__()
self.initUI()
def initUI(self):
self.btn = QtGui.QPushButton('Button', self)
self.btn.resize(self.btn.sizeHint())
self.btn.move(50, 50)
class TestViewController():
def __init__(self, view):
view.btn.clicked.connect(self.buttonClicked)
view.show()
def buttonClicked(self):
print 'clicked'
def main():
app = QtGui.QApplication(sys.argv)
view = TestView()
TestViewController(view)
app.exec_()
if __name__ == '__main__':
main()
The example is supposed to represent an MVC structure (like the one in Figure 4 -- without the Model) where the controller (TestViewController) receives a reference to the view (TestView) and connects the clicked signal from the view's button view.btn to its function self.buttonClicked.
I'm sure the line view.btn.clicked.connect(self.buttonClicked) is executed but, apparently, it has no effect. Does anyone knows how to solve that?
Update (awful solution):
In the example, if I replace the line
view.btn.clicked.connect(self.buttonClicked)
with
view.clicked = self.clicked
view.btn.clicked.connect(view.clicked)
it works. I'm still not happy with that.
The reason it is not working is because the controller class is being garbage collected before you can ever click anything for it.
When you set view.clicked = self.clicked, what you're actually doing is making one of the objects from the controller persist on the view object so it never gets cleaned up - which isn't really the solution.
If you store your controller to a variable, it will protect it from collection.
So if you change your code above to read:
ctrl = TestViewController(view)
You'll be all set.
That being said - what exactly you are trying to do here, I am not sure...it seems you're trying to setup an MVC system for Qt - but Qt already has a pretty good system for that using the Qt Designer to separate the interface components into UI (view/template) files from controller logic (QWidget subclasses). Again, I don't know what you are trying to do and this may be a dumb down version of it, but I'd recommend making it all one class like so:
from PyQt4 import QtGui
import sys
class TestView(QtGui.QWidget):
def __init__(self):
super(TestView, self).__init__()
self.initUI()
def initUI(self):
self.btn = QtGui.QPushButton('Button', self)
self.btn.resize(self.btn.sizeHint())
self.btn.move(50, 50)
self.btn.clicked.connect(self.buttonClicked)
def buttonClicked(self):
print 'clicked'
def main():
app = QtGui.QApplication(sys.argv)
view = TestView()
view.show()
app.exec_()
if __name__ == '__main__':
main()
Edit: Clarifying the MVC of Qt
So this above example doesn't actually load the ui dynamically and create a controller/view separation. Its a bit hard to show on here. Best to work through some Qt/Designer based examples/tutorials - I have one here http://bitesofcode.blogspot.com/2011/10/introduction-to-designer.html but many can be found online.
The short answer is, your loadUi method can be replace with a PyQt4.uic dynamic load (and there are a number of different ways to set that up) such that your code ultimately reads something like this:
from PyQt4 import QtGui
import PyQt4.uic
import sys
class TestController(QtGui.QWidget):
def __init__(self):
super(TestController, self).__init__()
# load view
uifile = '/path/to/some/widget.ui'
PyQt4.uic.loadUi(uifile, self)
# create connections (assuming there is a widget called 'btn' that is loaded)
self.btn.clicked.connect(self.buttonClicked)
def buttonClicked(self):
print 'clicked'
def main():
app = QtGui.QApplication(sys.argv)
view = TestController()
view.show()
app.exec_()
if __name__ == '__main__':
main()
Edit 2: Storing UI references
If it is easier to visualize this concept, you Can also store a reference to the generated UI object:
from PyQt4 import QtGui
import PyQt4.uic
import sys
class TestController(QtGui.QWidget):
def __init__(self):
super(TestController, self).__init__()
# load a view from an external template
uifile = '/path/to/some/widget.ui'
self.ui = PyQt4.uic.loadUi(uifile, self)
# create connections (assuming there is a widget called 'btn' that is loaded)
self.ui.btn.clicked.connect(self.buttonClicked)
def buttonClicked(self):
print 'clicked'
def main():
app = QtGui.QApplication(sys.argv)
view = TestController()
view.show()
app.exec_()
if __name__ == '__main__':
main()