RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1592407
Accepted
dynamic.aerospace.inc
dynamic.aerospace.inc
Asked:2024-08-30 23:27:32 +0000 UTC2024-08-30 23:27:32 +0000 UTC 2024-08-30 23:27:32 +0000 UTC

在 Qt-Designer 中创建活动文件面板

  • 772

不久前,我在编写程序时开始学习PyQt,并面临一个问题:
如何制作活动文件或项目的列表?

当您打开PyCharm、Sublime Text甚至Notepad时,您可以在顶部看到当前打开工作的文件列表。
是否可以使用PyQt来实现这一点?如果可以,如何实现?

小部件示例

这样的小部件应该打开、关闭和显示其内部的任何程序元素


更新:

我们目前拥有的:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <author>Dynamic Software, Dynamic Aerospace</author>
 <class>IIC</class>
 <widget class="QMainWindow" name="IIC">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>640</width>
    <height>480</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>IIC - (демо версия)</string>
  </property>
  <widget class="QWidget" name="centralwidget"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>640</width>
     <height>22</height>
    </rect>
   </property>
   <widget class="QMenu" name="File">
    <property name="title">
     <string>Файл</string>
    </property>
    <addaction name="New"/>
    <addaction name="Open"/>
    <addaction name="RecentFile"/>
    <addaction name="separator"/>
    <addaction name="Close"/>
    <addaction name="CloseAll"/>
    <addaction name="separator"/>
    <addaction name="Save"/>
    <addaction name="SaveAs"/>
    <addaction name="Export"/>
    <addaction name="separator"/>
    <addaction name="Preview"/>
    <addaction name="Print"/>
    <addaction name="separator"/>
    <addaction name="Send"/>
    <addaction name="separator"/>
    <addaction name="InformationDocument"/>
    <addaction name="separator"/>
    <addaction name="Exit"/>
   </widget>
   <widget class="QMenu" name="Edit">
    <property name="title">
     <string>Правка</string>
    </property>
    <addaction name="Undo"/>
    <addaction name="Redo"/>
    <addaction name="separator"/>
    <addaction name="RepeatLastCommand"/>
    <addaction name="separator"/>
    <addaction name="Cut"/>
    <addaction name="Copy"/>
    <addaction name="Paste"/>
    <addaction name="Delete"/>
    <addaction name="separator"/>
    <addaction name="Scale"/>
   </widget>
   <widget class="QMenu" name="Settings">
    <property name="title">
     <string>Настройки</string>
    </property>
    <addaction name="RestoreLicenses"/>
    <addaction name="ReceiveLicensesIIC"/>
    <addaction name="separator"/>
    <addaction name="Parameters"/>
    <addaction name="LoadParameters"/>
    <addaction name="SaveParameters"/>
   </widget>
   <widget class="QMenu" name="Apps">
    <property name="title">
     <string>Приложения</string>
    </property>
    <addaction name="AddApps"/>
    <addaction name="Configurator"/>
   </widget>
   <widget class="QMenu" name="Window">
    <property name="title">
     <string>Окно</string>
    </property>
    <addaction name="CloseCurrentWindow"/>
   </widget>
   <widget class="QMenu" name="Help">
    <property name="title">
     <string>Справка</string>
    </property>
    <addaction name="Content"/>
    <addaction name="separator"/>
    <addaction name="TrainingMaterials"/>
    <addaction name="separator"/>
    <addaction name="IICOnInternet"/>
    <addaction name="separator"/>
    <addaction name="LicenseAgreement"/>
    <addaction name="AboutProgram"/>
   </widget>
   <widget class="QMenu" name="Decoration">
    <property name="title">
     <string>Оформление</string>
    </property>
    <addaction name="StyleLibrary"/>
    <addaction name="separator"/>
    <addaction name="CreateReport"/>
    <addaction name="EditTemplateReport"/>
    <addaction name="CreateTemplateReport"/>
   </widget>
   <widget class="QMenu" name="Analysis">
    <property name="title">
     <string>Анализ</string>
    </property>
    <addaction name="AnalysisInjector"/>
    <addaction name="OptimizationInjector"/>
    <addaction name="separator"/>
    <addaction name="CustomExpressions"/>
   </widget>
   <addaction name="File"/>
   <addaction name="Edit"/>
   <addaction name="Analysis"/>
   <addaction name="Decoration"/>
   <addaction name="Settings"/>
   <addaction name="Apps"/>
   <addaction name="Window"/>
   <addaction name="Help"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <action name="New">
   <property name="text">
    <string>Новый</string>
   </property>
  </action>
  <action name="Open">
   <property name="text">
    <string>Открыть</string>
   </property>
  </action>
  <action name="RecentFile">
   <property name="text">
    <string>Последние файлы</string>
   </property>
  </action>
  <action name="Save">
   <property name="text">
    <string>Сохранить</string>
   </property>
  </action>
  <action name="SaveAs">
   <property name="text">
    <string>Сохранить как</string>
   </property>
  </action>
  <action name="action_PDF">
   <property name="text">
    <string>.PDF</string>
   </property>
  </action>
  <action name="action_TXT">
   <property name="text">
    <string>.TXT</string>
   </property>
  </action>
  <action name="Export">
   <property name="text">
    <string>Экспортировать</string>
   </property>
  </action>
  <action name="Print">
   <property name="text">
    <string>Печать</string>
   </property>
  </action>
  <action name="Exit">
   <property name="text">
    <string>Выход</string>
   </property>
  </action>
  <action name="Undo">
   <property name="text">
    <string>Отменить</string>
   </property>
  </action>
  <action name="Redo">
   <property name="text">
    <string>Повторить</string>
   </property>
  </action>
  <action name="Cut">
   <property name="text">
    <string>Вырезать</string>
   </property>
  </action>
  <action name="Copy">
   <property name="text">
    <string>Копировать</string>
   </property>
  </action>
  <action name="Paste">
   <property name="text">
    <string>Вставить</string>
   </property>
  </action>
  <action name="action_6">
   <property name="text">
    <string>Дублировать</string>
   </property>
  </action>
  <action name="Delete">
   <property name="text">
    <string>Удалить</string>
   </property>
  </action>
  <action name="Scale">
   <property name="text">
    <string>Масштабировать</string>
   </property>
  </action>
  <action name="RestoreLicenses">
   <property name="text">
    <string>Восстановить лицензии</string>
   </property>
  </action>
  <action name="ReceiveLicensesIIC">
   <property name="text">
    <string>Получить лицензию на IIC</string>
   </property>
  </action>
  <action name="Parameters">
   <property name="text">
    <string>Параметры</string>
   </property>
  </action>
  <action name="LoadParameters">
   <property name="text">
    <string>Загрузить параметры</string>
   </property>
  </action>
  <action name="SaveParameters">
   <property name="text">
    <string>Сохранить параметры</string>
   </property>
  </action>
  <action name="AddApps">
   <property name="text">
    <string>Добавить приложение</string>
   </property>
  </action>
  <action name="Configurator">
   <property name="text">
    <string>Конфигуратор</string>
   </property>
  </action>
  <action name="CloseCurrentWindow">
   <property name="text">
    <string>Закрыть текущую вкладку</string>
   </property>
  </action>
  <action name="Content">
   <property name="text">
    <string>Содержание</string>
   </property>
  </action>
  <action name="TrainingMaterials">
   <property name="text">
    <string>Обучающие материалы</string>
   </property>
  </action>
  <action name="IICOnInternet">
   <property name="text">
    <string>IIC в интернете</string>
   </property>
  </action>
  <action name="LicenseAgreement">
   <property name="text">
    <string>Лицензионное соглашение</string>
   </property>
  </action>
  <action name="AboutProgram">
   <property name="text">
    <string>О программе</string>
   </property>
  </action>
  <action name="InformationDocument">
   <property name="text">
    <string>Информация о документе</string>
   </property>
  </action>
  <action name="Send">
   <property name="text">
    <string>Отправить</string>
   </property>
  </action>
  <action name="Close">
   <property name="text">
    <string>Закрыть</string>
   </property>
  </action>
  <action name="CloseAll">
   <property name="text">
    <string>Закрыть все документы</string>
   </property>
  </action>
  <action name="Preview">
   <property name="text">
    <string>Предварительный просмотр</string>
   </property>
  </action>
  <action name="RepeatLastCommand">
   <property name="text">
    <string>Повторить последнюю команду</string>
   </property>
  </action>
  <action name="StyleLibrary">
   <property name="text">
    <string>Библиотека стилей</string>
   </property>
  </action>
  <action name="CreateReport">
   <property name="text">
    <string>Создать отчет</string>
   </property>
  </action>
  <action name="EditTemplateReport">
   <property name="text">
    <string>Редактировать шаблон отчета</string>
   </property>
  </action>
  <action name="CreateTemplateReport">
   <property name="text">
    <string>Создать шаблон отчета</string>
   </property>
  </action>
  <action name="AnalysisInjector">
   <property name="text">
    <string>Анализ форсунки</string>
   </property>
  </action>
  <action name="OptimizationInjector">
   <property name="text">
    <string>Оптимизация форсунки</string>
   </property>
  </action>
  <action name="CustomExpressions">
   <property name="text">
    <string>Пользовательские выражения</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

为了清晰起见,截图:

在此输入图像描述

python
  • 1 1 个回答
  • 41 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2024-09-04T21:25:10Z2024-09-04T21:25:10Z

    我不太确定我是否正确理解了你的意思,但尝试检查一个可能的选项:

    1. 将您的.ui表单转换为.py:

      pyuic5 q1592407.ui -o q1592407_ui.py -x

    2. 执行main.py:

    。

    import sys
    from PyQt5.Qt import *
    from q1592407_ui import Ui_IIC
    
    
    lineBarColor       = QColor(53, 53, 53)       
    lineHighlightColor = QColor('#00FF04')   
    
    
    class NumberBar(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.editor = parent
            layout = QVBoxLayout(self)
            self.editor.blockCountChanged.connect(self.update_width)
            self.editor.updateRequest.connect(self.update_on_scroll)
            self.update_width('001')   
    
        def mousePressEvent(self, QMouseEvent):
            print("\n - class NumberBar(): \n\tdef mousePressEvent():")
    
        def update_on_scroll(self, rect, scroll):
            if self.isVisible():
                if scroll:
                    self.scroll(0, scroll)
                else:
                    self.update()
    
        def update_width(self, string):
            width = self.fontMetrics().width(str(string)) + 10
            if self.width() != width:
                self.setFixedWidth(width)
    
        def paintEvent(self, event):
            if self.isVisible():
                block   = self.editor.firstVisibleBlock()
                height  = self.fontMetrics().height()
                number  = block.blockNumber()
                painter = QPainter(self)
                painter.fillRect(event.rect(), lineBarColor)
                painter.setPen(Qt.white)                       
                painter.drawRect(
                    0, 0, 
                    event.rect().width() - 1, 
                    event.rect().height() - 1)
                font = painter.font()
    
                current_block = self.editor.textCursor().block().blockNumber() + 1
    
                while block.isValid():
                    block_geometry = self.editor.blockBoundingGeometry(block)
                    offset = self.editor.contentOffset()
                    block_top = block_geometry.translated(offset).top()
                    number += 1
    
                    rect = QRect(0, block_top, self.width() - 5, height)
    
                    if number == current_block:
                        font.setBold(True)
                    else:
                        font.setBold(False)
    
                    painter.setFont(font)
                    painter.drawText(rect, Qt.AlignRight, '%i' % number)
    
                    if block_top > event.rect().bottom():
                        break
    
                    block = block.next()
                painter.end()
                
    
    class Content(QWidget):
        def __init__(self, text):
            super(Content, self).__init__()
            self.editor = QPlainTextEdit()
            self.editor.setPlainText(text)
            self.hbox    = QHBoxLayout(self)
            self.numbers = NumberBar(self.editor)
            self.hbox.addWidget(self.numbers)
            self.hbox.addWidget(self.editor)
            
    
    class TabWidget(QTabWidget):
        def __init__(self, parent=None):
            super(TabWidget, self).__init__(parent)
      
        def tabRemoved(self, index):
            print(f"\n вкладка удалена из индекса позиции -> {index}")
    
        def tabInserted(self, index):
            print(f"\n Новая вкладка была добавлена или вставлена "
                f"в индекс позиции -> {index}")
        
    
    class MyTableWidget(QWidget):
        def __init__(self, parent=None):
            super(QWidget, self).__init__(parent)
            
            self.layout = QVBoxLayout(self)
            # Initialize tab screen
            self.tabs = TabWidget()                  
            self.tabs.resize(300, 200)
    
            # Add tabs
            self.tabs.setTabsClosable(True)
            self.tabs.tabCloseRequested.connect(self.closeTab)
    
            # Add tabs to widget
            self.layout.addWidget(self.tabs)
            self.setLayout(self.layout)
    
        def closeTab(self, index):
            tab = self.tabs.widget(index)
            tab.deleteLater()
            self.tabs.removeTab(index)
    
        def addtab(self, content, fileName):
            self.tabs.addTab(Content(str(content)), str(fileName))
    
    
    class MainWindow(QMainWindow, Ui_IIC):
        def __init__(self):
            super().__init__()
            
            self.setupUi(self)
    
            self.tabs = MyTableWidget()
            
            self.layout = QVBoxLayout(self.centralwidget)
            self.layout.addWidget(self.tabs)
    
            self.Open.triggered.connect(self.openFile)
            self.New.triggered.connect(self.new)
    
        def new(self):
            self.tabs.addtab('', 'new_file_name.txt')                     
    
        def openFile(self):                                   
            options = QFileDialog.Options()
            filenames, _ = QFileDialog.getOpenFileNames(
                self, 'Open a file', '',
                'Python Files (*.py);;Text Files (*.txt)',
                options=options
            )
            if filenames:
                for filename in filenames:
                    with open(filename, 'r+') as file_o:
                        try: 
                            text = file_o.read()
                            self.tabs.addtab(text, filename)      
                        except Exception as e:
                            print("Error: filename=`{}`, `{}` ".format( filename, str(e)))
            
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec())
    

    在此输入图像描述

    在此输入图像描述

    在此输入图像描述

    在此输入图像描述

    在此输入图像描述

    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5