RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1275104
Accepted
hyper-cookie
hyper-cookie
Asked:2022-04-26 22:24:02 +0000 UTC2022-04-26 22:24:02 +0000 UTC 2022-04-26 22:24:02 +0000 UTC

如何在 PyQt5 中的图片上显示表单?

  • 772

可用的:

一款类似于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_())

图片字段:

在此处输入图像描述

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2022-04-27T02:14:48Z2022-04-27T02:14:48Z

    试试这样:

    import sys
    from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, \
        QVBoxLayout, QLineEdit, QFormLayout, QGroupBox, QRadioButton, \
        QMainWindow, QWidget, QGridLayout, QApplication, QSizePolicy
    from PyQt5.QtCore import Qt, QSize
    from PyQt5.QtGui import QFont, QPainter, QColor, QPixmap, QIcon
    
    WINDOW_HEIGHT = 600
    WINDOW_WIDTH = 600
    WINDOW_NAME = 'Истребитель уток'
    WINDOW_ICON_NAME = 'Images\\main_logo.png'
    #WINDOW_ICON_NAME = 'Ok.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 Label(QLabel):
        def __init__(self, path, w, h):
            super().__init__()
            self.setPixmap(QPixmap(path).scaled(w, h))
    
    
    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()
            
    # +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv   
            self.my_widget = QWidget(self.form_builder_object.background_image_label)
            self.my_widget.setFixedWidth(self.size().width()-30)
    
            self.layout_left = QHBoxLayout()
            for i in range(2):
                label = Label("bullet.png", 10, 40) 
                self.layout_left.addWidget(label) 
            self.layout_left.addStretch(1)
            
            self.layout_center = QHBoxLayout() 
            self.layout_center.addStretch(1)
            self.button = QPushButton() 
            self.button.setFixedWidth(25)
            self.button.setIcon(QIcon("down.png"))
            self.button.setIconSize(QSize(20, 20))
            self.layout_center.addWidget(self.button)
            self.layout_center.addStretch(1)
            
            self.layout_right = QHBoxLayout()
            self.layout_right.addStretch(1)
            for i in range(2):
                label = Label("heart_.png", 30, 30) 
                self.layout_right.addWidget(label) 
                
            my_layout = QHBoxLayout(self.my_widget)      
            my_layout.addLayout(self.layout_left, 0)
            my_layout.addLayout(self.layout_center, 1)
            my_layout.addLayout(self.layout_right, 0)
            
            
            label = Label("bullet.png", 10, 40) 
            self.layout_left.insertWidget(0, label)
            
            label = Label("heart_.png", 30, 30) 
            self.layout_right.addWidget(label) 
    # +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
        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)
            self.form_builder_object = FormBuilder()                               # + self.
            self.main_grid.addWidget(self.form_builder_object, 0, 0, Qt.AlignTop)  # + self. 
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = MainWindow()
        ex.show()
        sys.exit(app.exec_())
    

    在此处输入图像描述

    • 1

相关问题

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

  • telebot.anihelper.ApiException 错误

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

  • 解析多个响应

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

Sidebar

Stats

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

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 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