你能告诉我这些值是什么意思:样本噪声和采样偏差?这是书中的完整段落:
使用代表您希望概括的示例的训练集至关重要。实现这个目标往往比看起来要困难得多:如果样本太小,那么就会得到采样噪声(sampling noise),即 不具代表性的数据是偶然的结果,但如果抽样方法有误,即使是非常大的样本也可能不具代表性。这称为抽样偏差。
看完后,问题来了:“如果样本太小”/“大样本”是什么意思?抽样方法”;
你能告诉我这些值是什么意思:样本噪声和采样偏差?这是书中的完整段落:
使用代表您希望概括的示例的训练集至关重要。实现这个目标往往比看起来要困难得多:如果样本太小,那么就会得到采样噪声(sampling noise),即 不具代表性的数据是偶然的结果,但如果抽样方法有误,即使是非常大的样本也可能不具代表性。这称为抽样偏差。
看完后,问题来了:“如果样本太小”/“大样本”是什么意思?抽样方法”;
我想扩展机器的数据集,由 1100 个实例组成,将在使用对象检测算法训练神经网络时使用。我有两个问题,有人可以帮忙吗?
问题:
我需要为我的图片创建多少个额外的增强实例?我的意思是,我有 1100 个实例,我是否需要对每张图片/每第二张图片/每张图片多次应用图像增强?
究竟应该如何使用对象检测算法对神经网络进行图像增强?只是把车的图片移到一边不行吗?据我了解,您可以镜像、移开、旋转、放大。你还能尝试什么?
任务:用户插入 Instagram 帖子的链接,程序的任务是从那里解析帖子的文本。同时,您不能使用需要您的 Instagram 帐户/需要登录才能使用 API 的方法。也就是说,您需要一个库或方法,其方法允许您通过链接从帖子中获取文本。
问题:尝试了不同的方法,没有任何效果。我只能获取N个用户帖子的数据,表明他的ID(这也是不可能得到的)。请帮帮我。
PS。instagram-private-api库的方法media_info
(理论上可以给出帖子的正文)给出如下错误HTTPError "Method Not Allowed" while opening https://www.instagram.com/query/
可用:字符串user_str = " Привет как ДЕЛА? "
。您需要删除两边的空格并删除字母的大写。
问题:是否可以像这样执行此任务user_str.strip().lower()
?或者您是否需要依次应用这些方法user_str.strip()
并在下一行应用该方法lower()
?在我看来,第一个选项不是很漂亮。
有一项任务:创建一种方法来解析两个(到目前为止)社交网络中的帖子。网络。
问题:请告诉我,创建一个函数是否更好,该函数将传递一个带有社交名称的属性。网络,并且它已经使用条件来决定接下来将启动哪个算法(示例parse_post(social_network)
或创建两个函数来解析每个社交网络(示例parse_VK_post
和parse_TG_post
)?
描述了他们的工作原理,但为什么这样命名还不清楚。请帮帮我。
我有:使用 KNN 分类器构建 GridSearchCV 的代码。
从 sklearn.neighbors 导入 KNeighborsClassifier 从 sklearn.model_selection 导入 GridSearchCV param_grid = [{'weights': ["uniform", "distance"], 'n_neighbors': [3, 4, 5, 6, 7, 8, 9]}] KNN_clf = KNeighborsClassifier() grid_search = GridSearchCV(KNN_clf, param_grid, cv=5, verbose=3) grid_search.fit(train_data_values,train_data_labels)
问题:我不明白,我可以grid_search
在训练后作为 KNN 模型使用还是需要调用best_params
grid_search 方法,将它们拿在新的 KNN 分类器中使用?谁能解释一下?
例如:有一个文件夹,里面有一个对象的图片,图片格式是png。我想教分类器(我使用 scikit-learn,Python)来识别这个对象,但我不知道将什么作为一组数据和标签传递给fit方法。
求助,我第二次问这个问题了,最后一个没有人回答。知道如何去做是非常重要的。
确切的问题:
可用:一个用PyQt5编写的游戏程序。当您单击按钮Начать игру
时,会出现鸭子,它们是使用Duck
QGraphics 字段上的类创建的:
问题:这些图片(鸭子)的背景不是透明的,虽然图片格式是PNG
.
请帮我让鸭子数据的背景透明。
代码:
import sys
import random
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QFormLayout, QGroupBox, \
QRadioButton, QMainWindow, QWidget, QGridLayout, QApplication, QGraphicsScene, QGraphicsView, QGraphicsItem, \
QGraphicsPixmapItem
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QFont, QPainter, QColor, QPixmap, QIcon, QBrush, QPen
WINDOW_HEIGHT = 600
WINDOW_WIDTH = 600
WINDOW_NAME = 'Истребитель уток'
WINDOW_ICON_NAME = 'Images\\main_logo.png'
MAIN_FONT = "Times"
FONT_HEADER_SIZE = 13
FONT_BODY_SIZE = 8
DUCKS_QTY = 8
class Duck(QGraphicsPixmapItem): # класс утки
def __init__(self):
super().__init__()
duck_pixmap = QPixmap("goose.png") # загрузка картинки
self.setPixmap(duck_pixmap)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setPos(random.choice([-110, 600]), random.choice([230, 250, 270]))
self.duck_reverse_x = "Left"
self.duck_reverse_y = "Up"
class FormBuilder(QWidget):
ducks = []
duck_move_timer = QTimer()
def __init__(self, parent=None):
super().__init__(parent)
self.draw_brush = QBrush(Qt.green)
self.draw_pen = QPen(Qt.red)
self.duck_move_timer.setInterval(20)
self.duck_move_timer.timeout.connect(self.timeStep)
self.create_all_forms()
self.fill_forms()
self.create_layers()
def create_all_forms(self):
self.task_label = QLabel()
self.control_label = QLabel()
self.start_button = QPushButton()
self.quit_button = QPushButton()
self.quit_button.clicked.connect(self.duck_move_timer.stop)
self.quit_button.clicked.connect(self.game_stopped)
self.mode_label = QLabel()
self.first_rbutton = QRadioButton()
self.first_rbutton.clicked.connect(self.set_timer_interval)
self.second_rbutton = QRadioButton()
self.second_rbutton.clicked.connect(self.set_timer_interval)
self.third_rbutton = QRadioButton()
self.third_rbutton.clicked.connect(self.set_timer_interval)
self.results_label = QLabel()
self.play_time_qedit = QLineEdit(placeholderText="Время игры")
self.play_attempts_qedit = QLineEdit(placeholderText="Число попыток")
self.qgraphic_background_image = QPixmap("backimage.jpg")
self.background_graph_scene = QGraphicsScene()
self.background_graph_scene.selectionChanged.connect(
self.handle_qgraphic_selection)
self.graphic_view = QGraphicsView(self.background_graph_scene)
self.graphic_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphic_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphic_view.setFixedSize(565, 385)
self.background_graph_scene.addPixmap(self.qgraphic_background_image)
def fill_forms(self):
self.task_label.setText("Задача: убейте всех уток за меньшее число попыток.")
self.task_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.task_label.setAlignment(Qt.AlignCenter)
self.control_label.setText("Управление")
self.start_button.setText("Начать игру")
self.start_button.clicked.connect(self.build_qgraphic_ducks)
self.start_button.clicked.connect(self.game_started)
self.quit_button.setText("Закончить игру")
self.mode_label.setText("Режим:")
self.mode_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.first_rbutton.setText("1-я скорость")
self.first_rbutton.setChecked(True)
self.second_rbutton.setText("2-я скорость")
self.third_rbutton.setText("3-я скорость")
self.results_label.setText("Результаты:")
self.results_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.play_time_qedit.setDisabled(True)
self.play_attempts_qedit.setDisabled(True)
def create_layers(self):
outline = '''
QGroupBox {
margin-top: 2ex;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 3ex;
}
'''
main_layout = QVBoxLayout(self)
task_layout = QHBoxLayout()
task_layout.addWidget(self.task_label)
main_layout.addLayout(task_layout)
self.control_groupbox = QGroupBox(self.control_label.text())
self.control_groupbox.setStyleSheet(outline)
control_groupbox_layout = QHBoxLayout(self.control_groupbox)
control_groupbox_layout.addWidget(self.start_button)
control_groupbox_layout.addWidget(self.quit_button)
main_layout.addWidget(self.control_groupbox)
self.mode_groupbox = QGroupBox(self.mode_label.text())
self.mode_groupbox.setStyleSheet(outline)
mode_groupbox_layout = QHBoxLayout(self.mode_groupbox)
mode_groupbox_layout.addWidget(self.first_rbutton)
mode_groupbox_layout.addWidget(self.second_rbutton)
mode_groupbox_layout.addWidget(self.third_rbutton)
main_layout.addWidget(self.mode_groupbox)
self.result_groupbox = QGroupBox(self.results_label.text())
self.result_groupbox.setStyleSheet(outline)
result_groupbox_layout = QHBoxLayout(self.result_groupbox)
result_groupbox_layout.addWidget(self.play_time_qedit)
result_groupbox_layout.addWidget(self.play_attempts_qedit)
main_layout.addWidget(self.result_groupbox)
background_image_layout = QHBoxLayout()
background_image_layout.addWidget(self.graphic_view)
main_layout.addLayout(background_image_layout)
def game_started(self):
rbuttons = [self.first_rbutton, self.second_rbutton, self.third_rbutton]
for rbutton in rbuttons:
rbutton.setDisabled(True)
self.start_button.setDisabled(True)
def game_stopped(self):
rbuttons = [self.first_rbutton, self.second_rbutton, self.third_rbutton]
for rbutton in rbuttons:
rbutton.setDisabled(False)
self.start_button.setDisabled(False)
def build_qgraphic_ducks(self):
print('Starting')
self.remove_qgraphic_ducks()
for duck_number in range(DUCKS_QTY):
duck = Duck()
self.ducks.append(duck)
self.background_graph_scene.addItem(duck)
self.duck_move_timer.start()
def remove_qgraphic_ducks(self):
self.ducks = []
all_qgraphic_objects = self.background_graph_scene.items()
for qgraphic_object in all_qgraphic_objects:
if type(qgraphic_object) != QGraphicsPixmapItem:
self.background_graph_scene.removeItem(qgraphic_object)
def handle_qgraphic_selection(self):
all_qgraphic_objects = self.background_graph_scene.selectedItems()
if len(all_qgraphic_objects) != 0:
for qgraphic_object in all_qgraphic_objects:
self.background_graph_scene.removeItem(all_qgraphic_objects[0])
def set_timer_interval(self):
rbuttons = [self.first_rbutton, self.second_rbutton, self.third_rbutton]
rbutton_num = 1
for rbutton in rbuttons:
if rbutton.isChecked() and rbutton_num == 1:
self.duck_move_timer.setInterval(20)
self.duck_move_timer.timeout.connect(self.timeStep)
elif rbutton.isChecked() and rbutton_num == 2:
self.duck_move_timer.setInterval(50)
self.duck_move_timer.timeout.connect(self.timeStep)
elif rbutton.isChecked() and rbutton_num == 3:
self.duck_move_timer.setInterval(100)
self.duck_move_timer.timeout.connect(self.timeStep)
rbutton_num += 1
def timeStep(self):
for duck in range(DUCKS_QTY):
if self.ducks[duck].pos().x() <= 35 or self.ducks[duck].duck_reverse_x == "Right":
self.ducks[duck].setPos(self.ducks[duck].pos().x() + random.randint(0, 5), self.ducks[duck].pos().y())
if self.ducks[duck].pos().x() >= 480:
self.ducks[duck].duck_reverse_x = "Left"
elif self.ducks[duck].pos().x() >= 565 or self.ducks[duck].duck_reverse_x == "Left":
self.ducks[duck].setPos(self.ducks[duck].pos().x() - random.randint(0, 5), self.ducks[duck].pos().y())
if self.ducks[duck].pos().x() <= 35:
self.ducks[duck].duck_reverse_x = "Right"
if self.ducks[duck].pos().y() <= 10 or self.ducks[duck].duck_reverse_y == "Down":
self.ducks[duck].setPos(self.ducks[duck].pos().x(), self.ducks[duck].pos().y() + random.randint(0, 3))
if self.ducks[duck].pos().y() >= 300:
self.ducks[duck].duck_reverse_y = "Up"
elif self.ducks[duck].pos().y() >= 300 or self.ducks[duck].duck_reverse_y == "Up":
self.ducks[duck].setPos(self.ducks[duck].pos().x(), self.ducks[duck].pos().y() - random.randint(0, 3))
if self.ducks[duck].pos().y() <= 10:
self.ducks[duck].duck_reverse_y = "Down"
self.background_graph_scene.update()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
self.setWindowIcon(QIcon(WINDOW_ICON_NAME))
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
一张鸭子的照片,签名如下goose.png
:
背景图像,将其标记为backimage.jpg
:
可用的:
一款类似于猎鸭的未完成软件游戏。表格有4行:开始和停止游戏,选择速度模式,结果,最底部有一个用于未来鸭子的QGraphic字段。当您按下按钮Начать игру
时,会出现 8 个椭圆(鸭子),当您按下它们时,它们会消失。当您再次按下该按钮时,Начать игру
旧的椭圆(鸭子)会被删除并出现新的椭圆。
问题:
您如何实现这些椭圆(鸭子)在整个字段上的“飞行”,以便所有其他功能(单击按钮、单击鸭子本身等)保持活动状态?可以使用setPos
使用随机坐标的函数移动鸭子。如果你使用 function time.sleep()
,那么整个程序就会挂起。
您能告诉我如何实施吗?我听说过一些关于溪流的事情,但对我来说这是一片黑森林。
代码:(所有动作都发生在类FormBuilder
中,用于实现动作的函数用注释标记)
import sys
import random
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QFormLayout, QGroupBox, \
QRadioButton, QMainWindow, QWidget, QGridLayout, QApplication, QGraphicsScene, QGraphicsView, QGraphicsItem, \
QGraphicsPixmapItem
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QPainter, QColor, QPixmap, QIcon, QBrush, QPen
WINDOW_HEIGHT = 600
WINDOW_WIDTH = 600
WINDOW_NAME = 'Истребитель уток'
WINDOW_ICON_NAME = 'Images\\main_logo.png'
MAIN_FONT = 'Times'
FONT_HEADER_SIZE = 13
FONT_BODY_SIZE = 8
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.draw_brush = QBrush(Qt.green)
self.draw_pen = QPen(Qt.red)
self.create_all_forms()
self.fill_forms()
self.create_layers()
def create_all_forms(self):
self.task_label = QLabel()
self.control_label = QLabel()
self.start_button = QPushButton()
self.quit_button = QPushButton()
self.mode_label = QLabel()
self.first_rbutton = QRadioButton()
self.second_rbutton = QRadioButton()
self.third_rbutton = QRadioButton()
self.results_label = QLabel()
self.play_time_qedit = QLineEdit(placeholderText="Время игры")
self.play_attempts_qedit = QLineEdit(placeholderText="Число попыток")
self.qgraphic_background_image = QPixmap("backimage.jpg")
self.background_graph_scene = QGraphicsScene()
self.background_graph_scene.selectionChanged.connect(
self.handle_qgraphic_selection)
self.graphic_view = QGraphicsView(self.background_graph_scene)
self.graphic_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphic_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphic_view.setFixedSize(565, 385)
self.background_graph_scene.addPixmap(self.qgraphic_background_image)
def fill_forms(self):
self.task_label.setText("Задача: убейте всех уток за установленное время.")
self.task_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.task_label.setAlignment(Qt.AlignCenter)
self.control_label.setText("Управление")
self.start_button.setText("Начать игру")
self.start_button.clicked.connect(self.build_qgraphic_ducks)
self.quit_button.setText("Закончить игру")
self.mode_label.setText("Режим:")
self.mode_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.first_rbutton.setText("1-я скорость")
self.first_rbutton.setChecked(True)
self.second_rbutton.setText("2-я скорость")
self.third_rbutton.setText("3-я скорость")
self.results_label.setText("Результаты:")
self.results_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.play_time_qedit.setDisabled(True)
self.play_attempts_qedit.setDisabled(True)
def create_layers(self):
outline = '''
QGroupBox {
margin-top: 2ex;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 3ex;
}
'''
main_layout = QVBoxLayout(self)
task_layout = QHBoxLayout()
task_layout.addWidget(self.task_label)
main_layout.addLayout(task_layout)
self.control_groupbox = QGroupBox(self.control_label.text())
self.control_groupbox.setStyleSheet(outline)
control_groupbox_layout = QHBoxLayout(self.control_groupbox)
control_groupbox_layout.addWidget(self.start_button)
control_groupbox_layout.addWidget(self.quit_button)
main_layout.addWidget(self.control_groupbox)
self.mode_groupbox = QGroupBox(self.mode_label.text())
self.mode_groupbox.setStyleSheet(outline)
mode_groupbox_layout = QHBoxLayout(self.mode_groupbox)
mode_groupbox_layout.addWidget(self.first_rbutton)
mode_groupbox_layout.addWidget(self.second_rbutton)
mode_groupbox_layout.addWidget(self.third_rbutton)
main_layout.addWidget(self.mode_groupbox)
self.result_groupbox = QGroupBox(self.results_label.text())
self.result_groupbox.setStyleSheet(outline)
result_groupbox_layout = QHBoxLayout(self.result_groupbox)
result_groupbox_layout.addWidget(self.play_time_qedit)
result_groupbox_layout.addWidget(self.play_attempts_qedit)
main_layout.addWidget(self.result_groupbox)
background_image_layout = QHBoxLayout()
background_image_layout.addWidget(self.graphic_view)
main_layout.addLayout(background_image_layout)
def build_qgraphic_ducks(self):
self.remove_qgraphic_ducks()
for duck_number in range(8):
ellipse = self.background_graph_scene.addEllipse(
random.randint(5, 550), random.randint(5, 350), 30, 25, self.draw_pen, self.draw_brush)
ellipse.setFlag(QGraphicsItem.ItemIsSelectable)
def remove_qgraphic_ducks(self):
all_qgraphic_objects = self.background_graph_scene.items()
for qgraphic_object in all_qgraphic_objects:
if type(qgraphic_object) != QGraphicsPixmapItem:
self.background_graph_scene.removeItem(qgraphic_object)
def move_qgraphic_ducks(self): # реализация движения уток
pass
def handle_qgraphic_selection(self):
all_qgraphic_objects = self.background_graph_scene.selectedItems()
if len(all_qgraphic_objects) != 0:
for qgraphic_object in all_qgraphic_objects:
# ss[0].setPos(10, 10)
self.background_graph_scene.removeItem(all_qgraphic_objects[0])
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
self.setWindowIcon(QIcon(WINDOW_ICON_NAME))
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
QGraphic 字段的背景图像:
我有一个 PyQt5程序,它QGraphic
在该字段上绘制一个 -field 和一个椭圆。属性分配给这个椭圆Selectable
,当你用鼠标点击它时,它被选中,但我不知道如何处理这个选择。也就是说,当你点击这个椭圆时,会返回一个信号,或者说点击了某个椭圆。
请告诉我,这怎么办?
节目类型:
代码:(所有动作都发生在课堂上FormBuilder
)
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QFormLayout, QGroupBox, \
QRadioButton, QMainWindow, QWidget, QGridLayout, QApplication, QGraphicsScene, QGraphicsView, QGraphicsItem
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush, QPen
WINDOW_HEIGHT = 150
WINDOW_WIDTH = 600
WINDOW_NAME = 'QGraphics Test'
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.create_qgraphics()
self.create_layers()
def create_qgraphics(self): # создаем и настраиваем QGraph
self.background_graph_scene = QGraphicsScene()
self.brush = QBrush(Qt.green)
self.pen = QPen(Qt.red)
self.graphic_view = QGraphicsView(self.background_graph_scene)
self.graphic_view.setGeometry(0, 0, 10, 10)
# добавляем эллипс для примера и делаем его selectable
ellipse = self.background_graph_scene.addEllipse(20, 20, 50, 50, self.pen, self.brush)
ellipse.setFlag(QGraphicsItem.ItemIsSelectable)
def create_layers(self): # подключаем QGraph к Grid
main_layout = QVBoxLayout(self)
background_image_layout = QHBoxLayout()
background_image_layout.addWidget(self.graphic_view)
main_layout.addLayout(background_image_layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
可用的:
一款类似于Duck Hunt的未完成软件游戏。有 3 行形状:开始和停止游戏,选择速度模式和结果。最底部是未来鸭子的图片字段。
问题:
我把鸭子的实现留在按钮上(我把鸭子的图片放在按钮上)然后我会移动这个按钮,这不是重点。一般来说,当你尝试在这张图片上画一个按钮(为未来的鸭子)=它出现在右边(附图片),而不是图片本身,里面的字段。
请帮我纠正这个错误,也就是让鸭子按钮可以放在图片上(代码和图片字段本身会在下面附上)。
编码:
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QFormLayout, QGroupBox, QRadioButton, QMainWindow, QWidget, QGridLayout, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QPainter, QColor, QPixmap, QIcon
WINDOW_HEIGHT = 600
WINDOW_WIDTH = 600
WINDOW_NAME = 'Истребитель уток'
WINDOW_ICON_NAME = 'Images\\main_logo.png'
MAIN_FONT = 'Times'
FONT_HEADER_SIZE = 13
FONT_BODY_SIZE = 8
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.create_all_forms()
self.fill_forms()
self.create_layers()
def create_all_forms(self):
self.task_label = QLabel()
self.control_label = QLabel()
self.start_button = QPushButton()
self.quit_button = QPushButton()
self.mode_label = QLabel()
self.first_rbutton = QRadioButton()
self.second_rbutton = QRadioButton()
self.third_rbutton = QRadioButton()
self.results_label = QLabel()
self.play_time_qedit = QLineEdit(placeholderText="Время игры")
self.play_attempts_qedit = QLineEdit(placeholderText="Число попыток")
self.background_image_label = QLabel()
background_image = QPixmap("backimage.png")
self.background_image_label.setPixmap(background_image)
def fill_forms(self):
self.task_label.setText("Задача: убейте всех уток за установленное время.")
self.task_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.task_label.setAlignment(Qt.AlignCenter)
self.control_label.setText("Управление")
self.start_button.setText("Начать игру")
self.quit_button.setText("Закончить игру")
self.mode_label.setText("Режим:")
self.mode_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.first_rbutton.setText("1-я скорость")
self.first_rbutton.setChecked(True)
self.second_rbutton.setText("2-я скорость")
self.third_rbutton.setText("3-я скорость")
self.results_label.setText("Результаты:")
self.results_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.play_time_qedit.setDisabled(True)
self.play_attempts_qedit.setDisabled(True)
def create_layers(self):
outline = '''
QGroupBox {
margin-top: 2ex;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 3ex;
}
'''
main_layout = QVBoxLayout(self)
task_layout = QHBoxLayout()
task_layout.addWidget(self.task_label)
main_layout.addLayout(task_layout)
self.control_groupbox = QGroupBox(self.control_label.text())
self.control_groupbox.setStyleSheet(outline)
control_groupbox_layout = QHBoxLayout(self.control_groupbox)
control_groupbox_layout.addWidget(self.start_button)
control_groupbox_layout.addWidget(self.quit_button)
main_layout.addWidget(self.control_groupbox)
self.mode_groupbox = QGroupBox(self.mode_label.text())
self.mode_groupbox.setStyleSheet(outline)
mode_groupbox_layout = QHBoxLayout(self.mode_groupbox)
mode_groupbox_layout.addWidget(self.first_rbutton)
mode_groupbox_layout.addWidget(self.second_rbutton)
mode_groupbox_layout.addWidget(self.third_rbutton)
main_layout.addWidget(self.mode_groupbox)
self.result_groupbox = QGroupBox(self.results_label.text())
self.result_groupbox.setStyleSheet(outline)
result_groupbox_layout = QHBoxLayout(self.result_groupbox)
result_groupbox_layout.addWidget(self.play_time_qedit)
result_groupbox_layout.addWidget(self.play_attempts_qedit)
main_layout.addWidget(self.result_groupbox)
background_image_layout = QHBoxLayout()
background_image_layout.addWidget(self.background_image_label)
main_layout.addLayout(background_image_layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
self.setWindowIcon(QIcon(WINDOW_ICON_NAME))
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
图片字段:
有一个程序преобразования температуры из градусов цельсия в фаренгейт и обратно
。在第一个输入字段中,用户必须输入一个值,单击所需的按钮进行转换,转换结果应出现在下面的表格中(带有题字答案):
此外,在结果以第二种形式出现后,应以最低绘制形式绘制一个矩形(比例),其长度将等于温度变换的结果:
它应该是这样的:
但我做不到。代码会在下面,现在我给大家讲一下这段代码绘制的大致原理:类中
的方法一直被激活(这段代码我是从别的网站复制过来的,原理我也不是很懂这样的工作,但一般意思是这样),当程序第一次启动时()方法启动,它绘制“温度计主体”并将变量更改为。paintEvent
FormBuilder
self.launch = "first"
draw_thermometer
self.launch
"second"
现在,当方法被激活时paintEvent
,方法被执行fill_thermometer
,但是温度计的“刻度”(如上图,红色刻度)的绘制不会发生。
请帮我解决这个问题。如果有任何帮助,我将非常高兴!
PS 代码的重要部分(负责绘制)用注释标记# важный метод!
导入系统 从 PyQt5.QtWidgets 导入 QWidget、QPushButton、QLabel、QHBoxLayout、QVBoxLayout、QLineEdit、QFormLayout、\ QGroupBox、QMainWindow、QGridLayout、QApplication 从 PyQt5.QtCore 导入 Qt 从 PyQt5.QtGui 导入 QFont、QPainter、QColor、QPen、QIcon 窗口高度 = 210 窗口宽度 = 300 WINDOW_NAME = '温度' WINDOW_ICON_NAME = '图片\\main_logo.png' MAIN_FONT = '时间' FONT_HEADER_SIZE = 13 FONT_BODY_SIZE = 8 类FormBuilder(QWidget): def __init__(self, parent=None): super().__init__(父) self.launch = "第一" self.create_all_forms() self.fill_forms() self.create_layers() def create_all_forms(self): self.heading_label = QLabel() self.entering_label = QLabel() self.first_value = QLineEdit(placeholderText="Value T") self.second_value = QLineEdit(placeholderText="Answer") self.celc_button = QPushButton("转摄氏度") self.far_button = QPushButton("到华氏度") self.first_draw_label = QLabel(" ") self.second_draw_label = QLabel(" ") 定义填充表格(自我): self.heading_label.setText("CF 温度换算") self.heading_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE, QFont.Bold)) self.entering_label.setText("温度值") self.entering_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE)) self.second_value.setDisabled(True) self.celc_button.setMinimumWidth(125) self.celc_button.clicked.connect(self.make_celc) self.far_button.clicked.connect(self.make_far) self.first_draw_label.resize(500, 500) def create_layers(self): main_layout = QVBoxLayout(self) 标题标签 = QHBoxLayout() heading_label.addWidget(self.heading_label) main_layout.addLayout(heading_label) self.first_groupbox = QGroupBox(self.entering_label.text()) self.first_groupbox.setStyleSheet(''' QGroupBox { 保证金顶部:2ex; } QGroupBox:启用{ 边框:3px 纯灰色; 边框半径:5px } QGroupBox::title { subcontrol-origin:边距; 左:3ex; } ''') first_groupbox_layout = QVBoxLayout(self.first_groupbox) first_groupbox_layout.addWidget(self.first_value) first_groupbox_layout.addWidget(self.second_value) main_layout.addWidget(self.first_groupbox) button_line = QFormLayout() button_line.addRow(self.celc_button, self.far_button) main_layout.addLayout(button_line) first_draw_label = QHBoxLayout() first_draw_label.addWidget(self.first_draw_label) main_layout.addLayout(first_draw_label) second_draw_label = QHBoxLayout() second_draw_label.addWidget(self.second_draw_label) main_layout.addLayout(second_draw_label) def paintEvent(self, e): # 重要方法! qp = QPainter() qp.开始(自我) 如果 self.launch == “第一”: self.draw_thermometer(qp) elif self.launch == “第二”: self.fill_thermometer(qp) qp.end() def draw_thermometer(self, qp): # 重要方法! col = QColor(0, 0, 0) col.setNamedColor('#d4d4d4') qp.setPen(col) qp.setBrush(QColor(112, 106, 104)) qp.drawRoundedRect(10, 150, 260, 35, 5, 5) qp.setBrush(QColor(194, 189, 186)) qp.drawRect(15, 155, 250, 25) self.launch = 2 def make_celc(self): 尝试: 如果浮动(self.first_value.text())212: self.first_value.setText("212") self.second_value.setText("答案") 结果 = round((float(self.first_value.text()) - 32) * (5 / 9), 3) self.second_value.setText(str(result)) self.launch = "第二" 除了: self.second_value.setText("无效输入!") def make_far(self): 尝试: 如果浮动(self.first_value.text())100: self.first_value.setText("100") self.second_value.setText("答案") 结果 = round((float(self.first_value.text()) * (9 / 5)) + 32, 3) self.second_value.setText(str(result)) self.launch = "第二" 除了: self.second_value.setText("无效输入!") def fill_thermometer(self, qp): # 重要方法! col = QColor(0, 0, 0) col.setNamedColor('#d4d4d4') qp.setPen(col) qp.setBrush(QColor(109, 160, 201)) rect_lenght = round(float(self.second_value.text())) qp.drawRect(17, 157, rect_lenght, 21) 类主窗口(QMainWindow): def __init__(self): 超级().__init__() self.centralWidget = QWidget() self.setCentralWidget(self.centralWidget) self.build_skeleton_with_basic_settings() self.create_and_connect_grid() def build_skeleton_with_basic_settings(self): self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT) self.setWindowTitle(WINDOW_NAME) self.setWindowIcon(QIcon(WINDOW_ICON_NAME)) def create_and_connect_grid(self): self.main_grid = QGridLayout(self.centralWidget) form_builder_object = FormBuilder() self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop) 如果 __name__ == '__main__': 应用程序 = QApplication(sys.argv) 例如 = 主窗口() ex.show() sys.exit(app.exec_())
你能告诉我为什么是真的吗?"09/03/2021 00:00" > "07/04/2021 00:00"
比较时,答案是假的。"06/03/2021 00:00" > "07/04/2021 00:00"
有一个简单的折线图(下图)。
你能告诉我如何给曲线下的所有东西上色吗?如何获得这样的阴影“山”。
这是代码:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["01.02.2002", "01.02.2003", "01.02.2004", "01.02.2005", "01.02.2006", "01.03.2006", "01.02.2007",
"01.02.2008", "01.02.2009", "01.02.2010", "01.02.2011", "01.02.2012", "01.02.2013", "01.02.2014"])
y = np.array([25, 71, 61, 33, 11, 3, 1,
25, 31, 41, 33, 11, 25, 18])
plt.plot(x,y)
plt.xticks(rotation=90)
plt.show()
有一个用于输入文本的程序label
和表格。qlineedit
下面是左边的程序照片和右边的程序照片,上面Paint'е
画了一个框架(实际上,你需要像右图那样做):
我知道如何为label
带有文本的简单边框添加边框,但我不知道如何为label
+添加边框qlineedit
...请帮助。下面是程序代码:
导入系统 从 PyQt5.QtWidgets 导入 QMainWindow、QWidget、QGridLayout、QApplication 从 PyQt5.QtGui 导入 QIcon 从 PyQt5.QtWidgets 导入 QWidget、QPushButton、QLabel、QHBoxLayout、QVBoxLayout、\ QRadioButton、QButtonGroup、QMessageBox、QApplication、QLineEdit、QFormLayout 从 PyQt5.QtCore 导入 Qt 从 PyQt5.QtGui 导入 QFont、QIcon、QPixmap 窗口高度 = 450 窗口宽度 = 300 WINDOW_NAME = 'KWUR' WINDOW_ICON_NAME = '图片\\main_logo.png' MAIN_FONT = '时间' FONT_HEADER_SIZE = 13 FONT_BODY_SIZE = 8 类FormBuilder(QWidget): def __init__(self, parent=None): super().__init__(父) self.create_all_forms() self.fill_forms() self.create_layers() def create_all_forms(self): self.heading_label = QLabel() self.entering_label = QLabel() self.first_value = QLineEdit() self.second_value = QLineEdit() self.third_value = QLineEdit() self.find_button = QPushButton('查找') self.clear_button = QPushButton('清除') 定义填充表格(自我): self.heading_label.setText('二次解') self.heading_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE, QFont.Bold)) self.entering_label.setText('输入系数:') self.entering_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE)) self.first_value.setText('值 1') self.first_value.setFixedSize(265, 20) self.second_value.setText('值 2') self.second_value.setFixedSize(265, 20) self.third_value.setText('值 3') self.third_value.setFixedSize(265, 20) def create_layers(self): main_layout = QVBoxLayout(self) 标题标签 = QHBoxLayout() heading_label.addWidget(self.heading_label) main_layout.addLayout(heading_label) entry_label = QHBoxLayout() enter_label.addWidget(self.entering_label) main_layout.addLayout(entering_label) first_qline_edit = QHBoxLayout() first_qline_edit.addWidget(self.first_value) main_layout.addLayout(first_qline_edit) second_qline_edit = QHBoxLayout() second_qline_edit.addWidget(self.second_value) main_layout.addLayout(second_qline_edit) third_qline_edit = QHBoxLayout() third_qline_edit.addWidget(self.third_value) main_layout.addLayout(third_qline_edit) 线 = QFormLayout() line.addRow(self.find_button, self.clear_button) 类主窗口(QMainWindow): def __init__(self): 超级().__init__() self.centralWidget = QWidget() self.setCentralWidget(self.centralWidget) self.build_skeleton_with_basic_settings() self.create_and_connect_grid() def build_skeleton_with_basic_settings(self): self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT) self.setWindowTitle(WINDOW_NAME) self.setWindowIcon(QIcon(WINDOW_ICON_NAME)) def create_and_connect_grid(self): self.main_grid = QGridLayout(self.centralWidget) form_builder_object = FormBuilder() self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop) 如果 __name__ == '__main__': 应用程序 = QApplication(sys.argv) 例如 = 主窗口() ex.show() sys.exit(app.exec_())
描述:在 VK 中与 3 个以上的对话者进行了对话,我的小组中的一个机器人已添加到那里;该机器人已被授予所有权利,已选择最新版本的 longpool。
任务:您需要获取之前在对话中写入的所有消息。您能告诉我如何实施吗?我会很高兴得到任何帮助!
已尝试:已成功使用查询请求有关对话的信息method('messages.getConversationsById', {'peer_ids': 2000000000 + id, 'unread': 0, 'filter': 'all', 'extended': 1})
,但此查询不显示有关用户的 MESSAGES 的信息。
我尝试了请求messages.getHistory
,但它不起作用,因为请求发生在多用户对话中,而不是在群组私人消息中。
该请求也经过messages.getDialogs
了测试,但正如 VK 官方文档所说,它已经过时,最好不要使用它。