我有一个巨大的数据框。我想根据某个标准对一列中的值进行分组,并向另一列添加一个新值:对于number从 1000 到 1999列中的所有值,为该group列分配 1 。从 2000 年到 2999 年分配 2,以此类推。
为了更好地理解一个例子:我有数据框 df_test
number
0 1200
1 1300
2 1450
3 1555
4 2300
5 2341
6 2355
7 2800
8 3003
9 4010
Run Code Online (Sandbox Code Playgroud)
我想按照上面的解释对值进行分组,从而得到以下新数据框。
number group
0 1200 1
1 1300 1
2 1450 1
3 1555 1
4 2300 2
5 2341 2
6 2355 2
7 2800 2
8 3003 3
9 4010 4
Run Code Online (Sandbox Code Playgroud)
我用以下循环尝试过:
for i in range(len(df_test)):
if df_test.number[i] >= 1000 …Run Code Online (Sandbox Code Playgroud) 如何在弹出窗口中实现一个进度条,该窗口通过QThread监视来自所谓的 Worker 类(即时间/CPU 消耗任务)的运行函数的进度?
我已经检查了无数示例和教程,但进度条显示在弹出窗口中的事实似乎使一切变得更加困难。我相信我想要的是一件相当简单的事情,但我一直在失败,而且我的想法也用完了。
我有一个我想要实现的例子,它基于这个答案:
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal, QObject, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QProgressBar, QVBoxLayout
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Widget")
self.h_box = QHBoxLayout(self)
self.main_window_button = QPushButton("Start")
self.main_window_button.clicked.connect(PopUpProgressB)
self.h_box.addWidget(self.main_window_button)
self.setLayout(self.h_box)
self.show()
class Worker(QObject):
finished = pyqtSignal()
intReady = pyqtSignal(int)
@pyqtSlot()
def proc_counter(self): # A slot takes no params
for i in range(1, 100):
time.sleep(1)
self.intReady.emit(i)
self.finished.emit()
class PopUpProgressB(QWidget):
def …Run Code Online (Sandbox Code Playgroud) 我正在查看QT 网站上的数据可视化工具教程,其中有一个在以下位置创建菜单栏的示例QMainWindow:
self.menu = self.menuBar()
self.file_menu = self.menu.addMenu("File")
Run Code Online (Sandbox Code Playgroud)
这在 OSX 10.13.6 上对我不起作用。我还尝试使用QMenuBar创建自己的菜单栏,而不是使用带有的默认菜单栏QMainWindow:
menu_bar = QMenuBar()
menu_bar.addMenu('File')
self.setMenuBar(menu_bar)
Run Code Online (Sandbox Code Playgroud)
这也没有效果。我从未在我的应用程序的菜单栏中看到“文件”选项。我只是得到一个带有单个“python”选项的通用菜单栏。
我正在尝试将 GUI 调色板从深色更改为浅色。
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
from PyQt5.QtCore import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Dark"))
self.pushButton_2.setText(_translate("Form", "Light"))
self.pushButton.clicked.connect(self.changeSkinDark)
self.pushButton_2.clicked.connect(self.changeSkinLight)
def changeSkinDark(self):
darkpalette = QPalette()
darkpalette.setColor(QPalette.Window, QColor(41,44,51))
darkpalette.setColor(QPalette.WindowText, Qt.white)
darkpalette.setColor(QPalette.Base, QColor(15,15,15))
darkpalette.setColor(QPalette.AlternateBase, QColor(41,44,51))
darkpalette.setColor(QPalette.ToolTipBase, Qt.white)
darkpalette.setColor(QPalette.ToolTipText, Qt.white)
darkpalette.setColor(QPalette.Text, Qt.white)
darkpalette.setColor(QPalette.Button, …Run Code Online (Sandbox Code Playgroud) 我正在使用 PyQt5,并且创建了一个 QMessagebox,如果单击“是”按钮,我想打印一些内容。这是我的代码
self.messageBox = QMessageBox()
self.messageBox.setText("Are You Sure with Left Edge You Chosed?")
self.messageBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
if self.messageBox == QMessageBox.Yes:
self.confirmation = 1
print("Yess Clicked")
else:
self.confirmation = 0
self.messageBox.exec()
Run Code Online (Sandbox Code Playgroud) 最近,有用户询问如何将 Pika 与 Qt 一起使用,但没想到在我即将发布答案时该用户删除了,这让我有机会通过自动回答提出这个问题,我尝试公开各种解决方案:
在新的 Qt 5.15.0 中,有一种新的声明方式来注册 C++ 类型以在 QML 中使用。我按照Qt 帮助中给出的步骤(https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type)但它给了我以下错误:
/.../randomnumbergenerator.h:10: error: ‘QML_ELEMENT’ does not name a type
QML_ELEMENT
^~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
目前该类的定义是:
/.../randomnumbergenerator.h:10: error: ‘QML_ELEMENT’ does not name a type
QML_ELEMENT
^~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
编辑:我已经在.pro文件中添加了以下内容:
CONFIG += c++11 qmltypes
QML_IMPORT_NAME = SimpleRng
QML_IMPORT_MAJOR_VERSION = 1
Run Code Online (Sandbox Code Playgroud) 所以,我有这个简单的 PyQt5 代码,它本质上是一个文件浏览器。我需要能够选择任意文件或文件组(目录和所有子文件)。我想:
我本质上是在构建一个要处理的选定文件的列表。
import sys
from PyQt5.QtWidgets import QApplication, QFileSystemModel, QTreeView, QWidget, QVBoxLayout
from PyQt5.QtGui import QIcon
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 file system view - pythonspot.com'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.model = QFileSystemModel()
self.model.setRootPath('')
self.tree = QTreeView()
self.tree.setModel(self.model)
self.tree.setAnimated(False)
self.tree.setIndentation(20)
self.tree.setSortingEnabled(True)
self.tree.setWindowTitle("Dir View")
self.tree.resize(640, 480)
windowLayout = QVBoxLayout()
windowLayout.addWidget(self.tree)
self.setLayout(windowLayout)
self.show()
if __name__ == '__main__':
app …Run Code Online (Sandbox Code Playgroud) 环境:
Python 3.7.7
Qt5
Qt 设计器 5.11
问题:
我使用 Qt Designer 设计了一个 GUI,并在按钮中的标签和图标背景中添加了一些图像。我可以从 Qt 设计器软件中正确看到图像和图标。但是当我执行 python 脚本来加载 UI 时,我再也看不到图像了。
代码:
我的主要 python 脚本位于根文件夹中,ui、qrc 和图像文件位于文件夹“ui”中:
main.py
ui/config.qrc
ui/doc.qrc
ui/document.png
ui/gear.png
ui/logo.qrc
ui/logo_MyUI_300.jpg
ui/MyUI.ui
ui/play-button.png
ui/play.png
ui/question.png
ui/quit.png
ui/quit.qrc
ui/report.qrc
ui/run.qrc
ui/save.png
ui/save.qrc
ui/scan.qrc
ui/search.png
ui/seo-report.png
ui/support.qrc
ui/telegram.png
ui/telegram.qrc
ui/watch_videos.qrc
Run Code Online (Sandbox Code Playgroud)
主要.py:
from PyQt5 import QtWidgets, uic
gui = QtWidgets.QApplication([])
ui=uic.loadUi('ui/MyUI.ui')
ui.show()
gui.exec()
Run Code Online (Sandbox Code Playgroud)
我的用户界面:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author>MyUI.co</author>
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>580</height>
</rect>
</property> …Run Code Online (Sandbox Code Playgroud) 此代码不能使用命令 g++ -std=c++17 main.cpp 编译
#include <iostream>
#include <experimental/optional>
int main()
{
std::optional<int> x;
std::cout << "Hello World";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误如下:
有没有办法让这段代码编译?
python ×7
pyqt5 ×6
pyqt ×4
python-3.x ×3
pyside2 ×2
amqp ×1
c++ ×1
dataframe ×1
pandas ×1
pika ×1
qmessagebox ×1
qml ×1
qpalette ×1
qprogressbar ×1
qt ×1
qt-designer ×1
qt5 ×1
qthread ×1
std ×1
stdoptional ×1