我找不到一个很好的例子来说明如何使用 PyQt 计算两个对象之间的时间差QDateTimeEdit
在帮助下.dateTime
,可以从元组上的对象中提取出来QDateTimeEdit
。
有没有一种简单的方法来获得int
两个对象(或至少元组)之间的差异(以分钟、小时等为单位),而无需构建三层结构?
找到了计算对象中指定的当前和时间之间差异的示例QDateTimeEdit
试图用Pyside6 弄清楚。任务是通过单击按钮将小部件添加到layout
主窗口并为其分配序列号。
小部件还有一个删除按钮,按下该按钮实际上应该删除小部件,其余的小部件应该重新编号。
我现在正在尝试使用“ self.ui.layout_f.count
”,但是在这种情况下,例如,如果您删除了五个小部件中的第四个,然后再次单击添加按钮,那么将有两个带有数字的小部件5
。
据我了解,整个问题在于算法setText
,在将重新分配小部件编号的函数press_del_f
中。
主窗口.py:
from PySide6.QtWidgets import QMainWindow
from PySide6.QtCore import Slot
from ui_mainwindow import Ui_MainWindow
from form_f import Form_f
class MainWindow(QMainWindow):
def __init__ (self):
super(MainWindow, self).__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.ui.add_but_f.clicked.connect(self.addf)
@Slot() #Добавление виджета ф
def addf (self):
self.id_f = self.ui.layout_f.count () +1
form_f = Form_f(self.id_f)
self.ui.layout_f.addWidget(form_f)
form_f.delete_f.connect(self.delete_form_f)
@Slot(int) #Функция удаления виджета Ф
def delete_form_f(self, wid: int):
print(f'Виджет удален ({wid})f')
widget_f = self.sender()
self.ui.layout_f.removeWidget(widget_f)
widget_f.deleteLater()
form_f.py:
from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Slot, Signal
from base_ui.ui_form_f import Ui_Form_f
class Form_f(QWidget):
delete_f = Signal(int)
def __init__(self, id_widget_f: int, parent=None):
super(Form_f, self).__init__(parent)
self.ui = Ui_Form_f()
self.ui.setupUi(self)
self.id_widget_f = id_widget_f
self.ui.label_id_f.setText(str(id_widget_f))
self.ui.deleteButton_f.clicked.connect(self.press_del_f)
@Slot()
def press_del_f(self):
self.delete_f.emit(self.id_widget_f)
ui_mainwindow.py
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QGridLayout, QMainWindow, QPushButton,
QSizePolicy, QVBoxLayout, QWidget)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(319, 291)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(u"gridLayout")
self.layout = QVBoxLayout()
self.layout.setObjectName(u"layout")
self.gridLayout.addLayout(self.layout, 0, 0, 1, 1)
self.add_but = QPushButton(self.centralwidget)
self.add_but.setObjectName(u"add_but")
self.gridLayout.addWidget(self.add_but, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.add_but.setText(QCoreApplication.translate("MainWindow", u"PushButton", None))
ui_form_f.py
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QDoubleSpinBox, QGroupBox, QHBoxLayout,
QLabel, QPushButton, QSizePolicy, QSpacerItem,
QVBoxLayout, QWidget)
import res_rc
class Ui_Form_f(object):
def setupUi(self, Form_f):
if not Form_f.objectName():
Form_f.setObjectName(u"Form_f")
Form_f.resize(375, 63)
Form_f.setMinimumSize(QSize(375, 63))
Form_f.setMaximumSize(QSize(400, 66))
font = QFont()
font.setFamilies([u"FreeSerif"])
font.setPointSize(12)
Form_f.setFont(font)
self.verticalLayout_f = QVBoxLayout(Form_f)
self.verticalLayout_f.setObjectName(u"verticalLayout_f")
self.groupBox_f = QGroupBox(Form_f)
self.groupBox_f.setObjectName(u"groupBox_f")
self.horizontalLayout = QHBoxLayout(self.groupBox_f)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.label_id_f = QLabel(self.groupBox_f)
self.label_id_f.setObjectName(u"label_id_f")
self.label_id_f.setMinimumSize(QSize(135, 0))
font1 = QFont()
font1.setFamilies([u"FreeSerif"])
font1.setPointSize(17)
self.label_id_f.setFont(font1)
self.label_id_f.setLayoutDirection(Qt.LeftToRight)
self.label_id_f.setAlignment(Qt.AlignCenter)
self.horizontalLayout.addWidget(self.label_id_f)
self.horizontalSpacer_f = QSpacerItem(56, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.horizontalLayout.addItem(self.horizontalSpacer_f)
self.resultBox_f = QDoubleSpinBox(self.groupBox_f)
self.resultBox_f.setObjectName(u"resultBox_f")
self.resultBox_f.setMinimumSize(QSize(100, 0))
self.horizontalLayout.addWidget(self.resultBox_f)
self.deleteButton_f = QPushButton(self.groupBox_f)
self.deleteButton_f.setObjectName(u"deleteButton_f")
self.deleteButton_f.setMinimumSize(QSize(25, 25))
icon = QIcon()
icon.addFile(u":/close.png", QSize(), QIcon.Normal, QIcon.Off)
self.deleteButton_f.setIcon(icon)
self.horizontalLayout.addWidget(self.deleteButton_f)
self.verticalLayout_f.addWidget(self.groupBox_f)
self.retranslateUi(Form_f)
QMetaObject.connectSlotsByName(Form_f)
# setupUi
def retranslateUi(self, Form_f):
Form_f.setWindowTitle(QCoreApplication.translate("Form_f", u"Form", None))
self.groupBox_f.setTitle("")
self.label_id_f.setText("")
self.deleteButton_f.setText("")
主.py:
import sys
from PySide6.QtWidgets import QApplication
from mainwindow import MainWindow
from base_ui.ui_form_f import Ui_Form_f
if __name__ == '__main__':
app=QApplication()
window=MainWindow()
window.show()
sys.exit(app.exec())
我正在尝试创建类似于 word 文档解析器的东西。任务是在 docx 表格中找到一行指示组织的 TIN 并显示右侧单元格中的 10 位数字
可能的表格视图
我试过这样:
import os
import docx
import re
from docx import Document
doc = docx.Document("D:/Python/sample.docx")
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
print(cell.text)
WordINNfull=re.search(r"(ИНН)\s(\d{10})", cell.text)
print(WordINNfull)
输出是这样的
Полное и сокращенное наименование
Общество с ограниченной ответственностью «»
ООО «»
Организационно-правовая форма
Общество с ограниченной ответственностью
Адрес юридический
444444, Московская область, г. Москва…………………………………
Адрес местонахождения и почтовый адрес
444444, Московская область, г. Москва…………………………………
Руководитель (должность, ФИО)
Директор – Иванов Иван Иванович,
Действует на основании Устава
ОГРН
1111111111111
ОКПО
11111111
ОКАТО
11111111111
ОКТМО
11111111111
Номера контактных телефоном и факсов
8-800-555-35-35
Адрес электронной почты
ya@.ru
ИНН/КПП
ИНН 1111111111/ КПП 111111111
Полные банковские реквизиты
Сбербанк
р/сч 1111111111111111111111111
БИК 1111111111111111
к/с 11111111111111111111
None
>>>
试图改变模板。例如,它"\d"
返回re.Match object; span=(36, 37), match='4'
,但只有“TIN”模板返回 None。
请朝正确的方向踢!