I have a program with two windows, main and the settings.
When I run setText on a QLineEdit in the settings.py file, the new string is not in the GUI, and I can see the string before the setText code.
When I put the same code in the settingsUI file generated from Qt Designer, It works. But in the settings.py doesn't.
The settings file is the file that contains the SettingsWindow class and I can put the real python code in it.
The settingsUI file is the file that contains the GUI, I generated it with pyuic4 (or pyuic5).
This code works in settingsUI file:
self.browse_file.setText("safa")
But dosen't work in settings file.
--UPDATE--
import sys
from PyQt4 import QtCore, QtGui
from settingsui import Ui_Dialog
class SettingsWindow(QtGui.QDialog, Ui_Dialog):
def __init__(self):
QtGui.QDialog.__init__(self)
Ui_Dialog.__init__(self)
self.setupUi(self)
self.lineEdit.setText("safa")
print self.lineEdit.text()
After: self.lineEdit.setText("safa") , I can't see any text in the QLineEdit.
print self.lineEdit.text() outputs the text "safa"
The problem is in your mainwind.py file.
You try to use the following method for opening the dialog:
def buttonclicked(self):
Dialog = QtGui.QDialog()
u = settings.SettingsWindow()
u.setupUi(Dialog)
Dialog.exec_()
The reason why the text doesn't show, is because you are creating two dialogs. The second one (named u) has setText() called on it, but then gets thrown away without being shown.
Your method should instead look like this:
def buttonclicked(self):
dialog = settings.SettingsWindow()
dialog.exec_()
All the setup code for the SettingsWindow dialog is already inside its __init__ method, so all you need to do is create an instance of it.
PS:
In MainWindow.__init__ you have Ui_MainWindow.__init__(self), and in SettingsWindow.__init__ you have Ui_Dialog.__init__(self). These lines don't do anything useful, because the Ui_* classes are just simple subclasses of object. So those two lines could be removed.
Shouldn't you initialize your UI along these lines:
class SettingsWindow(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.lineEdit.setText("safa")
print self.ui.lineEdit.text()
This is how I do it all the time and works like a charm.
Related
This question already has answers here:
Make QLabel clickable
(4 answers)
Promote PyQt Widget
(1 answer)
Closed 1 year ago.
What I want: Drag a file into label area, and get the file directory.
The problem is: I have 2 Class, 1 is for drag&drop function, the other one is for creating ui window. I don't know how to apply the drag&drop function to the label inside the ui.
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QLabel
# class that add drag&drop function to QLabel
class Label_draghere(QLabel):
def __init__(self, parent):
super().__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
def dropEvent(self, event):
# print file directory
lines = []
for url in event.mimeData().urls():
lines.append('dir: %r' % url.toLocalFile())
print(lines)
# class that create ui window
class Win_excelCheck:
def __init__(self):
# load ui (there's only 1 Label named label_drag1 in the ui, nothing else)
self.ui = uic.loadUi('ui/label_win.ui')
self.ui.setAcceptDrops(True)
# This line is wrong, what I'm tring to do is apply Label_draghere Class to label_drag1 widget
self.ui.label_drag1 = Label_draghere(self.ui)
if __name__ == '__main__':
app = QApplication([])
mainWin = Win_excelCheck()
mainWin.ui.show()
app.exec_()
Code Result: This code has no error message showed up, but when I drag file into the Label, there's a red prohibition sign next to my cursor and doesn't allow me to drop anything.
img_drop_prohibited
My reference: I wrote this code by referring to this:
https://www.jb51.net/article/226682.htm .But all the tutorials use addWidget to create ui instead of uic.loadUi, and I need to use uic way, so I don't know how to modify the code.
Please help, really appreciate ^_^
I have created a small uic file with qt Designer in order to build a small gui using PyQt4. One of the elements of that gui is a simple textbox, where I set a string value (textbox is called RiskDate_Box). After setting this value in the GUI, I want to use it as a string variable in the following code (not seen here) . My problem is, that I am not able to store it, in the code seen below I tried it in two different ways ( store it as Riskdate1 and Riskdate2). After compiling the code, bothvariables are empty and not equal to the value I set in the GUI, e.g. '12.08.2012'. However, if I compile the script and after that only compile
Riskdate2=window.RiskDate_Box.toPlainText()
then the Riskdate set in the Gui is correctly assigned to the variable 'Riskdate2' as a string. Would be great if someone could help me with that issue.
from PyQt4 import QtCore, QtGui, uic
import sys
# Gui Code
qtCreatorFile = "untitled.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyWindow(QtGui.QDialog):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi("untitled.ui", self)
self.show()
self.RiskDate=self.RiskDate_Box.toPlainText()
if __name__ == '__main__':
app=QtGui.QApplication.instance()
app=0
app = QtGui.QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
window = MyWindow()
#Try 1 to store variable
Riskdate1=window.RiskDate
# Try 2 to store variable
Riskdate2=window.RiskDate_Box.toPlainText()
sys.exit(app.exec_())
you need to send s signal when text in your QPlainTextEdit is changed
void QPlainTextEdit::textChanged()
This signal is emitted whenever the document's content changes; for example, when text is inserted or deleted, or when formatting is applied.
you need to do something like:
self.RiskDate_Box.textChanged.connect(self.get_text)
self.show()
def get_text(self):
self.RiskDate = self.RiskDate_Box.toPlainText())
print (self.RiskDate)
I'm making a large program in Python and using PyQt for the GUI. The whole program is divided into different modules so that different people can work on it simultaneously without interfering with the other people's work.
I am working on 3 different modules right now. 1 is the main program window that handles the basic UI and assigns widgets so the main window (this is not important, just so you know why the code doesn't look like a full program.)
First is the widget:
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from CustomButton import HoverButton #just a custom button class
from CustomGif import LblLoadingGif #where things go wrong
class Page1(QtGui.QWidget):
def __init__(self, parent=None):
super(Page1, self).__init__(parent)
self.lbl1GIF = LblLoadingGif(self)
self.lbl1GIF.move(400, 45)
self.btnStart = HoverButton(self)
self.btnStart.setText('Start')
self.btnStart.move(35, 400)
self.btnStart.clicked.connect(self.actStartGif)
#the code below works, but then I can only perform 1 action with each button
#self.btnStart.clicked.connect(self.lbl1GIF.actStart)
def actStartGif(self):
self.lbl1GIF.actStart
The code for the custom GIF looks as follows:
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class LblLoadingGif(QtGui.QLabel):
def __init__(self, parent=None):
QtGui.QLabel.__init__(self, parent)
self.setStyleSheet('background: url();')
self.setScaledContents(True)
self.resize(100, 100)
self.movLoadGif = QtGui.QMovie('Resources_Images/Loading6.gif', QtCore.QByteArray())
self.movLoadGif.setCacheMode(QtGui.QMovie.CacheAll)
self.movLoadGif.setSpeed(100)
self.setMovie(self.movLoadGif)
self.hide()
def actStart(self, event):
#print('test1')
self.show()
self.movLoadGif.start()
def actStop(self, event):
#print('test1')
self.hide()
self.movLoadGif.stop()
So the problem is that I can use the actStart function just fine when I call it from the button click directly, but not when I call it through another function. I have used a lot of different variations of brackets, self, Page1 when calling the actStart of the custom gif from withing the actStartGif function.
Any help will be appreciated.
When you use connect it is necessary to pass the name of the function since internally it is in charge of calling it, in your case you have to call it directly so you will have to pass its parameters, in this case event:
self.lbl1GIF.actStart({your value for event})
I do not understand why you use event for what happens to you None:
def actStartGif(self):
self.lbl1GIF.actStart(None)
I have created a simple test program in Qt Designer that allows you to select a folder and display its contents on a window. It looks like this:
I have successfully converted the .ui file to .py without fail. Next, here is my code to run the program, aptly named main.py:
from PyQt4 import QtGui
import sys
import design
import os
class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
def _init_(self):
super(self._class_, self)._init_()
self.setupUI(self)
self.btnBrowse.clicked.connect(self.browse_folder)
def browse_folder(self):
self.listWidget.clear()
directory = QtGui.QFileDialog.getExistingDirectory(self,"Pick a Folder")
if directory:
for file_name in os.listdir(directory):
self.listWidget.addItem(file_name)
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
In my command prompt, I run the following code:
python main.py
It proceeds to load for a second or two, and then I get this:
Is there something that I am doing wrong? Why isn't my program showing up the way it should be? Any help is appreciated!
These lines are wrong:
def _init_(self):
super(self._class_, self)._init_()
Instead you want something like:
def __init__(self, parent=None):
super(ExampleApp, self).__init__(parent)
Note the double underscores, the different super argument, and passing parent to the super class. I can't test this right now, but it should be much closer to working.
By naming your __init__ method incorrectly it never would've been called. That explains why you get a window but not the one you designed.
I have a fairly simply PyQt question. (Python 3.4, PyQt 4.11.3, Qt 4.8.5) I built a very simple dialog using Qt Designer (Ui_Dialog). This object has a QPushButton, a QLineEdit, and a QListWidget. I wrote another object that inherits from Ui_Dialog, and sets up a returnPressed signal from QLineEdit that should add some text to the QListWidget. Unfortunately, this does not work.
Here's my code:
import sys
from PyQt4 import QtGui
from dialog import Ui_Dialog
class ImDialog(QtGui.QDialog, Ui_Dialog):
def __init__(self):
super(ImDialog, self).__init__()
self.setupUi(self)
self.lineEdit.returnPressed.connect(self.additem)
self.pushButton.clicked.connect(self.listWidget.clear)
def additem(self):
text = self.lineEdit.text()
print(text)
self.listWidget.insertItem(0, text)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = ImDialog()
ui.show()
sys.exit(app.exec_())
The text in the line editor prints fine to the terminal, but it is not added to the listWidget.
Interestingly, if I comment out the sys.exit line and run this in an IPython terminal, I can add as much text as I like to the listWidget without a problem.
[In 1]: %run that_program.py
[In 2]: ui.listWidget.insertItem(0, "Test") # This works fine
If anyone has any suggestions to get this to work (outside IPython), I would appreciate the help. Thanks
There is only one button in your dialog, and so it will become the auto-default. This means that whenever you press enter in the dialog, the button will receive a press event, even if it doesn't currently have the keyboard focus.
So the item does get added to the list-widget - it's just that it then immediately gets cleared by the auto-default button.
To fix this, reset the auto-default like so:
self.pushButton.setAutoDefault(False)
(NB: you can also change this property in Qt Designer).