要使用 PyQt6 创建一个文本框,其中文本框的内容为文件名,安全为确定,并且带有图标,可以按照以下步骤进行操作:

  1. 导入必要的模块:
from PyQt6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton, QIcon
  1. 创建一个主窗口类,并继承自 QMainWindow:
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('文件名')
        self.setGeometry(100, 100, 300, 200)
  1. 在主窗口类的初始化方法中创建一个文本框和一个按钮,并设置其属性:
        self.textbox = QLineEdit(self)
        self.textbox.setGeometry(50, 50, 200, 30)
        self.textbox.setText('文件名')

        self.button = QPushButton(self)
        self.button.setGeometry(100, 100, 100, 30)
        self.button.setText('确定')
        self.button.clicked.connect(self.button_clicked)
  1. 在按钮的点击事件中获取文本框的内容并进行处理:
    def button_clicked(self):
        filename = self.textbox.text()
        print('文件名为:', filename)
  1. 添加一个图标到主窗口:
        self.setWindowIcon(QIcon('icon.png'))
  1. 创建应用程序和主窗口实例,并运行应用程序:
app = QApplication([])
window = MainWindow()
window.show()
app.exec()

完整的代码示例:

from PyQt6.QtWidgets import QApplication, QMainWindow, QLineEdit, QPushButton, QIcon

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('文件名')
        self.setGeometry(100, 100, 300, 200)

        self.textbox = QLineEdit(self)
        self.textbox.setGeometry(50, 50, 200, 30)
        self.textbox.setText('文件名')

        self.button = QPushButton(self)
        self.button.setGeometry(100, 100, 100, 30)
        self.button.setText('确定')
        self.button.clicked.connect(self.button_clicked)

        self.setWindowIcon(QIcon('icon.png'))

    def button_clicked(self):
        filename = self.textbox.text()
        print('文件名为:', filename)

app = QApplication([])
window = MainWindow()
window.show()
app.exec()

注意:确保将代码中的icon.png替换为你自己的图标文件路径。

Python PyQt6 文本框输入文件名并使用确定按钮 - 带图标

原文地址: https://www.cveoy.top/t/topic/p40q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录