change QLabel and QPushButton visible automatically - python

i just try to set Label and pushbutton Visible True when a QlineEdit == 1 number at least
so i don't know what to type in if statment and how to make the Label and Pushbutton appears automatically without push a button or action excpet the 1 number at least in QLine Edit i hope if someone supplement the code for me
the code :
import PyQt5
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore , QtGui , uic
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QPropertyAnimation , Qt
class Ui(QWidget):
def __init__(self):
super(Ui , self).__init__()
uic.loadUi("login_page.ui" , self)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.show()
self.on_Run()
def on_Run(self):
self.label.setVisible(False)
self.Hide_Show_Password.setVisible(False)
def show_hide_pass(self):
#Below Code For Hide and Show the password
if self.Password.text() == :
self.label.setVisible(True)
self.Hide_Show_Password.setVisible(True)

In the example below I create a generic button, label, and line edit, and add them to a layout. Then I connect the QLineEdit.textChanged signal to your show_hide_pass method which sends the text contents of the widget each time the contents are edited.
I am not 100% sure if you were saying that you only wanted the widgets to be visible if there was at least 1 digit, or if the number 1 appeared in the line edit, so I just chose the former, and am checking the length of the text parameter in the if statement, but if you are wanting to check for the number one you can just change that line to if "1" in text:. instead.
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Ui(QWidget):
def __init__(self):
super(Ui , self).__init__()
self.setAttribute(Qt.WA_TranslucentBackground)
self.setWindowFlags(Qt.FramelessWindowHint)
# Add Layout and Widgets
self.layout = QVBoxLayout(self)
self.label = QLabel("Label")
self.layout.addWidget(self.label)
self.Password = QLineEdit()
self.layout.addWidget(self.Password)
self.Hide_Show_Password = QPushButton("HideShowPasswordButton")
self.layout.addWidget(self.Hide_Show_Password)
# Connect widget signals to they slot methds
self.Password.textChanged.connect(self.show_hide_pass)
self.on_Run()
def on_Run(self):
self.label.setVisible(False)
self.Hide_Show_Password.setVisible(False)
def show_hide_pass(self, text):
if len(text) > 0:
self.label.setVisible(True)
self.Hide_Show_Password.setVisible(True)
app = QApplication([])
window = Ui()
window.show()
app.exec_()

#musicamente rightly pointed out that loadUI will automatically hook up widget signals to your code. For PyQt5 to find your methods, you must use this naming scheme:
def on_<widget_name>_<signal_name>():
For a QLineEdit, you will often use the returnPressed signal that fires when user presses Return or Enter key. I don't remember if this signal fires when user presses Tab. Assuming your widget is named lineEdit, you can add this method to your UI class:
def on_lineEdit_returnPressed(self):
if self.lineEdit.text() == '1':
self.label.setVisible(True)
self.Hide_Show_Password.setVisible(True)

Related

Can we create new combo boxes after user clicks something from another combo box in PyQt?

I am working on a ui project that requires me to change a lot of things based on what option user selects from the combo box. In this code i was trying to create another combo box if user selects 'b' but it isn't working
What am i doing wrong?
import PyQt5
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python ")
self.setGeometry(450, 100, 600, 400)
self.UiComponents()
self.show()
def UiComponents(self):
items = ['a','b','c']
x=20
y=15
self.create_combo(items,x,y)
button = QPushButton("Click", self)
button.pressed.connect(self.ffind)
button.setGeometry(20,55,40,30)
def ffind(self):
connect = self.combo_box.currentText()
if connect == "b":
#print('you selected b')
list1 = ['1','2','3']
self.create_combo(list1, 200,15)
def create_combo(self, items, x, y):
#print("inside function")
self.combo_box = QComboBox(self)
self.combo_box.setGeometry(x, y, 150, 30)
self.combo_box.addItems(items)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
P.S: this is my first question on stackoverflow so forgive me if i missed something :/
I tried to create a function to create combo boxes everytime its called. It works just fine if i call it normally but I want to create that only if user selects the appropriate option from the first combo box.
I tried to call it inside a function which verifies whether the option selected by the user is the one i need (in this code its 'b') but it does nothing whatsoever.

If click Eyes icon Show/Hide password (QPushButton)

I'm trying to create a function in a register and login form using QLineEdit to show and hide password if click a QPushButton. I'm a beginner in Python, I'm just trying to do it but it's very hard... My attempt is not good because if I click the eye button the password is shown, but if click again to hide it does not work.
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
import pymysql
pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.eyepass_show()
self.eyepass_hide()
self.btn_show_pwd.clicked.connect(self.eyepass_hide)
self.btn_show_pwd.clicked.connect(self.eyepass_show)
def eyepass_show(self):
self.line_password.setEchoMode(QLineEdit.Normal)
print('show pass')
def eyepass_hide(self):
self.line_password.setEchoMode(QLineEdit.Password)
print('hide pass')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
form password hide/show eye:
is hiding password is show but if click again to hide not work
Instead of creating two separate methods as eyepass_show and eyepass_hide, you can create a single function and toggle the visibility. Also, you are trying to connect the same signal twice to two different methods by self.btn_show_pwd.clicked.connect(self.eyepass_hide)and self.btn_show_pwd.clicked.connect(self.eyepass_show)
Try something like this:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
import pymysql
pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.eyepass_show()
self.eyepass_hide()
self.btn_show_pwd.clicked.connect(self.toggleVisibility)
def toggleVisibility(self):
if self.line_password.echoMode()==QLineEdit.Normal:
self.line_password.setEchoMode(QLineEdit.Password)
else:
self.line_password.setEchoMode(QLineEdit.Normal)
# self.btn_show_pwd.clicked.connect(self.eyepass_hide)
# self.btn_show_pwd.clicked.connect(self.eyepass_show)
#
# def eyepass_show(self):
# self.line_password.setEchoMode(QLineEdit.Normal)
# print('show pass')
#
# def eyepass_hide(self):
# self.line_password.setEchoMode(QLineEdit.Password)
# print('hide pass')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Another possibility is to add a checkable QAction to the QLineEdit and connect to the toggled (or triggered) signal.
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
icon = QtGui.QIcon('eye-icon.png')
self.showPassAction = QtWidgets.QAction(icon, 'Show password', self)
self.line_password.addAction(
self.showPassAction, QtWidgets.QLineEdit.TrailingPosition)
self.showPassAction.setCheckable(True)
self.showPassAction.toggled.connect(self.showPassword)
def showPassword(self, show):
self.line_password.setEchoMode(
QtWidgets.QLineEdit.Normal if show else QtWidgets.QLineEdit.Password)
If you want to show the password only when the mouse is pressed, then don't connect to the toggled signal, but find the child QToolButton for that action and connect to the pressed and released instead. In this case, the action doesn't need to be checkable.
self.line_password.addAction(
self.showPassAction, QtWidgets.QLineEdit.TrailingPosition)
showPassButton = self.line_password.findChild(QtWidgets.QAbstractButton)
showPassButton.pressed.connect(lambda: self.showPassword(True))
showPassButton.released.connect(lambda: self.showPassword(False))
One of the great things about PyQt5 is that it will automatically connect signals to methods for you. If you have a button named <mybutton> and a method in your app named on_<mybutton>_clicked, the the loadUi will automatically connect. So the pattern is
on_<widget_name>_<signal>
If we apply this to your problem, you should make your show/hide button a toggle button. In Qt Designer, set the checkable property to True (or do btn_show_pwd.clicked.setChecked(True))
The code:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QLineEdit
import sys
#import pymysql
#pymysql.install_as_MySQLdb()
class MyWindow(QtWidgets.QMainWindow):
def __init__(self, maxWidth=None):
super(MyWindow, self).__init__()
uic.loadUi('test1.ui', self)
self.line_password.setEchoMode(QLineEdit.Password)
def on_btn_show_pwd_toggled(self, checked):
if checked:
self.line_password.setEchoMode(QLineEdit.Password)
else:
self.line_password.setEchoMode(QLineEdit.Normal)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())

How to capture PyQt5 QMainWindow losing focus

What I want to achieve: if a user clicks outside of the QMainWindow the window should hide.
How I tried to to tackle this problem: find a way to determine if the QMainWindow lost focus, and if so, hide the window using a followup function.
Unfortunately I can not totally grasp how to achieve this.
It can be done using the flag Qt::Popup but than I am not able to give any keyboard input to the widget my QMainWindow contains.
void QApplication::focusChanged(QWidget *old, QWidget *now)
This signal is emitted when the widget that has keyboard focus changed from old to now, i.e., because the user pressed the tab-key, clicked into a widget or changed the active window. Both old and now can be the null-pointer.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MyWin(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setFocus()
QtWidgets.qApp.focusChanged.connect(self.on_focusChanged)
#QtCore.pyqtSlot("QWidget*", "QWidget*")
def on_focusChanged(self, old, now):
if now == None:
print(f"\nwindow is the active window: {self.isActiveWindow()}")
# window lost focus
# do what you want
self.setWindowState(QtCore.Qt.WindowMinimized)
else: print(f"window is the active window: {self.isActiveWindow()}")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = MyWin()
MainWindow.show()
sys.exit(app.exec_())

QlineEdit::text() not returning the text entered when push button is clicked and the object name of that particular QlineEdit is lineEdit

I have created three LineEdits in my Alert.ui file. My first LineEdit object name is lineEdit from where I need to read the text when the pushButton is clicked. But when I checked by printing a word reach, I understood that it never enters the onChanged method even though the text has been changed in that lineEdit from blank to some value. I can't understand why this happens.
import MySQLdb
import sys
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "Alert.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.lineEdit=QtGui.QLineEdit(self)
print("reach")
self.pushButton.clicked.connect(self.pushButton_clicked)
def pushButton_clicked(self):
self.lineEdit = QtGui.QLineEdit(self)
self.lineEdit.textChanged.connect(self.onClicked)
def onClicked(self,text):
ID=text
print(ID)
if __name__==_main_:
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
I'm still not sure what's going on in your code, but from first glance I can spot the following issue:
You never connect your first QLineEdit to the onClicked method.

Query regarding Pyside

In the below mentioned example when I click on 'Help' submenu under 'View' menu multiple times its creating multiple windows. Can anyone tell me how to resolve this issue?
import sys
from PySide import Qt Gui
from PySide.QtCore import Qt
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.menu_bar()
def menu_bar(self):
helpAction = QtGui.QAction('&Help', self)
helpAction.setShortcut('Ctrl+H')
helpAction.triggered.connect(self.add_helpWindow)
menu = self.menuBar().addMenu('View')
menu.addAction(helpAction)
def add_helpWindow(self):
window = QtGui.QMainWindow(self)
window.setWindowTitle('New Window')
window.show()
if __name__ == '__main__':
import sys
app=QtGui.QApplication.instance()
if not app:
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(300, 300)
window.show()
sys.exit(app.exec_())
You help window is just a QMainWindow, which is not modal and there are no restrictions on the number that can exist. Hence why if you select the help option multiple times, you get multiple windows.
You likely want to use a QMessageBox which has its modal property set. While there is nothing forcing only one dialog to exist at a time, being modal means that the use can only interact with that window so long as it is open. Example:
from Pyside.QtGui import QMessageBox
def add_helpWindow(self):
help_dialog = QMessageBox.information(self, 'Help', 'Some Help Text Here')
help_dialog.setModal(True)
return help_dialog.exec_()
You can also get a more generic dialog box using QDialog, which is the parent class of QMessageBox.
If that's not the behavior you want, you'll need to manually track whether the user has opened that window before, and then connect a signal that is emitted when the user closes the help window to a slot that reset the existence tracker. Here is an example using a non-modal QDialog:
from Pyside.QtGui import QDialog
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.menu_bar()
self.help_open = False # Tracks if the help dialog is already open
def help_closed(self):
self.help_open = False
...
def add_helpWindow(self):
if not self.help_open:
self.help_open = True
help_dialog = QDialog(self)
# Any other setup code here
help_dialog.setModal(False)
help_dialog.accepted.connect(self.help_closed)
help_dialog.rejected.connect(self.help_closed)
help_dialog.show()

Categories

Resources