python - Clear QLineEdit on click event

Python - Clear QLineEdit on click event

To clear the contents of a QLineEdit when it is clicked, you need to connect the mousePressEvent of the QLineEdit to a custom function that clears its text. Here's how you can achieve this in PyQt5:

Step 1: Install PyQt5

If you haven't already installed PyQt5, you can install it using pip:

pip install PyQt5 

Step 2: Create a Custom QLineEdit

You'll create a subclass of QLineEdit and override its mousePressEvent method to clear its text when it is clicked.

Step 3: Use the Custom QLineEdit in Your Application

Use the custom QLineEdit in your PyQt5 application.

Here is the complete code to achieve this:

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QVBoxLayout, QWidget class ClearableQLineEdit(QLineEdit): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def mousePressEvent(self, event): self.clear() super().mousePressEvent(event) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('QLineEdit Clear on Click Example') # Create an instance of the custom QLineEdit self.line_edit = ClearableQLineEdit(self) self.line_edit.setPlaceholderText("Click to clear text") layout = QVBoxLayout() layout.addWidget(self.line_edit) container = QWidget() container.setLayout(layout) self.setCentralWidget(container) def main(): app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_()) if __name__ == "__main__": main() 

Explanation

  1. ClearableQLineEdit Class:

    • This class inherits from QLineEdit and overrides the mousePressEvent method.
    • In the overridden method, self.clear() is called to clear the text, and then the parent class's mousePressEvent is called to ensure the click event is still handled normally.
  2. MainWindow Class:

    • This class sets up the main window of the application.
    • An instance of ClearableQLineEdit is created and added to the layout.
    • The setPlaceholderText method is used to show a hint in the input field.
  3. main Function:

    • This function initializes the application, creates the main window, and starts the event loop.

Summary

This approach ensures that the QLineEdit is cleared whenever it is clicked, using a custom subclass to handle the event. You can easily integrate this custom QLineEdit into any PyQt5 application.

Examples

  1. Python PyQt5 clear QLineEdit on click event Description: Clear the content of a PyQt5 QLineEdit widget when clicked.

    from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout import sys class Window(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 300, 100) self.setWindowTitle('Clear QLineEdit on Click') layout = QVBoxLayout() self.line_edit = QLineEdit(self) self.line_edit.setPlaceholderText('Enter text...') self.line_edit.clicked.connect(self.clearLineEdit) layout.addWidget(self.line_edit) self.setLayout(layout) def clearLineEdit(self): self.line_edit.clear() if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) 
  2. Python Tkinter clear Entry on click event Description: Clear the content of a Tkinter Entry widget in Python when clicked.

    import tkinter as tk def clear_entry(event): entry.delete(0, tk.END) root = tk.Tk() entry = tk.Entry(root) entry.insert(0, 'Enter text...') entry.bind("<Button-1>", clear_entry) entry.pack() root.mainloop() 
  3. Python Kivy clear TextInput on click event Description: Clear the content of a Kivy TextInput widget in Python upon click.

    from kivy.app import App from kivy.uix.textinput import TextInput from kivy.uix.boxlayout import BoxLayout class ClearTextInputApp(App): def build(self): layout = BoxLayout(padding=10) self.text_input = TextInput(text='Enter text...') self.text_input.bind(on_touch_down=self.clear_text) layout.add_widget(self.text_input) return layout def clear_text(self, instance, touch): if self.text_input.collide_point(*touch.pos): self.text_input.text = '' if __name__ == '__main__': ClearTextInputApp().run() 
  4. Python wxPython clear TextCtrl on click event Description: Clear the content of a wxPython TextCtrl widget when clicked.

    import wx class ClearTextCtrlFrame(wx.Frame): def __init__(self): super().__init__(None, title='Clear TextCtrl on Click') panel = wx.Panel(self) sizer = wx.BoxSizer(wx.VERTICAL) self.text_ctrl = wx.TextCtrl(panel, value='Enter text...') self.text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.clear_text) sizer.Add(self.text_ctrl, 0, wx.ALL | wx.EXPAND, 10) panel.SetSizer(sizer) self.Show() def clear_text(self, event): self.text_ctrl.SetValue('') if __name__ == '__main__': app = wx.App(False) frame = ClearTextCtrlFrame() app.MainLoop() 
  5. Python PySide2 clear QLineEdit on click event Description: Clear the content of a PySide2 QLineEdit widget when clicked.

    from PySide2.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout import sys class Window(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 300, 100) self.setWindowTitle('Clear QLineEdit on Click') layout = QVBoxLayout() self.line_edit = QLineEdit(self) self.line_edit.setPlaceholderText('Enter text...') self.line_edit.mousePressEvent = self.clearLineEdit layout.addWidget(self.line_edit) self.setLayout(layout) def clearLineEdit(self, event): self.line_edit.clear() if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) 
  6. Python PyGTK clear Entry on click event Description: Clear the content of a PyGTK Entry widget in Python when clicked.

    import pygtk import gtk def clear_entry(widget, event): entry.set_text('') window = gtk.Window() window.connect('delete-event', gtk.main_quit) entry = gtk.Entry() entry.set_text('Enter text...') entry.connect('button-press-event', clear_entry) window.add(entry) window.show_all() gtk.main() 
  7. Python wxPython clear TextCtrl on mouse click event Description: Clear the content of a wxPython TextCtrl widget on mouse click event.

    import wx class ClearTextCtrlFrame(wx.Frame): def __init__(self): super().__init__(None, title='Clear TextCtrl on Mouse Click') panel = wx.Panel(self) sizer = wx.BoxSizer(wx.VERTICAL) self.text_ctrl = wx.TextCtrl(panel, value='Enter text...') self.text_ctrl.Bind(wx.EVT_LEFT_DOWN, self.clear_text) sizer.Add(self.text_ctrl, 0, wx.ALL | wx.EXPAND, 10) panel.SetSizer(sizer) self.Show() def clear_text(self, event): self.text_ctrl.SetValue('') if __name__ == '__main__': app = wx.App(False) frame = ClearTextCtrlFrame() app.MainLoop() 
  8. Python PyGTK clear Entry on mouse click event Description: Clear the content of a PyGTK Entry widget in Python on mouse click event.

    import pygtk import gtk def clear_entry(widget, event): entry.set_text('') window = gtk.Window() window.connect('delete-event', gtk.main_quit) entry = gtk.Entry() entry.set_text('Enter text...') entry.connect('button-press-event', clear_entry) window.add(entry) window.show_all() gtk.main() 
  9. Python PySide2 clear QLineEdit on mouse click event Description: Clear the content of a PySide2 QLineEdit widget in Python on mouse click event.

    from PySide2.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout import sys class Window(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 300, 100) self.setWindowTitle('Clear QLineEdit on Mouse Click') layout = QVBoxLayout() self.line_edit = QLineEdit(self) self.line_edit.setPlaceholderText('Enter text...') self.line_edit.mousePressEvent = self.clearLineEdit layout.addWidget(self.line_edit) self.setLayout(layout) def clearLineEdit(self, event): self.line_edit.clear() if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) 
  10. Python Kivy clear TextInput on mouse click event Description: Clear the content of a Kivy TextInput widget in Python on mouse click event.

    from kivy.app import App from kivy.uix.textinput import TextInput from kivy.uix.boxlayout import BoxLayout class ClearTextInputApp(App): def build(self): layout = BoxLayout(padding=10) self.text_input = TextInput(text='Enter text...') self.text_input.bind(on_touch_down=self.clear_text) layout.add_widget(self.text_input) return layout def clear_text(self, instance, touch): if self.text_input.collide_point(*touch.pos): self.text_input.text = '' if __name__ == '__main__': ClearTextInputApp().run() 

More Tags

timing ctypes maven-2 uml xcode10 internal-tables windows-phone brackets mapstruct calico

More Programming Questions

More Date and Time Calculators

More Housing Building Calculators

More Genetics Calculators

More Trees & Forestry Calculators