Bard is coming along

David Cortesi davecortesi at gmail.com
Wed May 3 20:15:53 BST 2023


I got my invite to try Google Bard, and after asking it some general
questions in other areas I tried its skill at writing PyQt code. Here is
one question and its response. The first two lines of the prompt were
suggested earlier when I was asking it about code prompts in general.

python=3.10
> qt=6.5
> Write a Python subclass of QPlainTextEdit that includes methods for
> implementing search and replace
>
Sure, here is a Python subclass of QPlainTextEdit that includes methods for
> implementing search and replace:
> Python
>
> import sysfrom PyQt6.QtWidgets import QPlainTextEdit
> class SearchReplacePlainTextEdit(QPlainTextEdit):
>     def __init__(self):
>         super().__init__()
>
>         # Create the search and replace dialog
>         self.search_replace_dialog = QDialog()
>         self.search_replace_line_edit = QLineEdit()
>         self.replace_line_edit = QLineEdit()
>         self.case_sensitive_checkbox = QCheckBox("Case sensitive")
>         self.whole_words_checkbox = QCheckBox("Whole words")
>         self.search_button = QPushButton("Search")
>         self.replace_button = QPushButton("Replace")
>         self.replace_all_button = QPushButton("Replace all")
>
>         # Layout the dialog
>         layout = QVBoxLayout()
>         layout.addWidget(self.search_replace_line_edit)
>         layout.addWidget(self.replace_line_edit)
>         layout.addWidget(self.case_sensitive_checkbox)
>         layout.addWidget(self.whole_words_checkbox)
>         layout.addWidget(self.search_button)
>         layout.addWidget(self.replace_button)
>         layout.addWidget(self.replace_all_button)
>         self.search_replace_dialog.setLayout(layout)
>
>         # Connect the buttons
>         self.search_button.clicked.connect(self.on_search)
>         self.replace_button.clicked.connect(self.on_replace)
>         self.replace_all_button.clicked.connect(self.on_replace_all)
>
>     def on_search(self):
>         # Get the search text
>         search_text = self.search_replace_line_edit.text()
>
>         # Get the replace text
>         replace_text = self.replace_line_edit.text()
>
>         # Set the search flags
>         search_flags = QTextDocument.FindFlag.NoHighlight
>         if self.case_sensitive_checkbox.isChecked():
>             search_flags |= QTextDocument.FindFlag.CaseSensitive
>         if self.whole_words_checkbox.isChecked():
>             search_flags |= QTextDocument.FindFlag.WholeWords
>
>         # Find the text
>         self.find(search_text, search_flags)
>
>     def on_replace(self):
>         # Get the search text
>         search_text = self.search_replace_line_edit.text()
>
>         # Get the replace text
>         replace_text = self.replace_line_edit.text()
>
>         # Replace the text
>         self.replace(search_text, replace_text)
>
>     def on_replace_all(self):
>         # Get the search text
>         search_text = self.search_replace_line_edit.text()
>
>         # Get the replace text
>         replace_text = self.replace_line_edit.text()
>
>         # Replace all instances of the search text with the replace text
>         self.replaceAll(search_text, replace_text)
>
>
> Use code with caution. Learn more <https://bard.google.com/faq#coding>
>
> This code creates a subclass of QPlainTextEdit that includes methods for
> implementing search and replace. The search and replace dialog has the
> following features:
>
>    - A search text box
>    - A replace text box
>    - A case sensitive checkbox
>    - A whole words checkbox
>    - A search button
>    - A replace button
>    - A replace all button
>
> When the user clicks the search button, the text in the search text box is
> searched for in the text editor. If the text is found, the cursor is moved
> to the found text. When the user clicks the replace button, the text in the
> replace text box is substituted for the found text. When the user clicks
> the replace all button, all instances of the search text are replaced with
> the replace text.
>
> I hope this is what you are looking for. Let me know if you have any other
> questions.
>
It looks good but when I run it only the edit widget is visible. Not sure
how it messed up the layout so the buttons etc. do not show.

Still, Bard gives a decent starting point.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20230503/b1ffc08d/attachment.htm>


More information about the PyQt mailing list