如何在PyQt5中将小部件并排安装?

huangapple go评论50阅读模式
英文:

How to install widgets next to each other in Pyqt5?

问题

以下是您的代码的部分翻译:

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QLineEdit, QVBoxLayout, QHBoxLayout, QSpacerItem, QSizePolicy
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QCursor, QFont
from PyQt5.QtCore import Qt

# ... 翻译的代码部分未包含在此回复中 ...

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = App()
sys.exit(app.exec_())

请注意,代码中的变量名和注释并没有翻译,只有代码本身被翻译成了中文。希望这可以帮助您更好地理解代码的结构。如果您需要更多的帮助,请随时提出具体的问题。

英文:

In my code there are 2 grids (with buttons and with text fields). The buttons work fine and are in their places, and the text fields, no matter what I do, fall into the upper left corner, and I need them to be to the left of the buttons \under each other\ in 1 column (in the screenshot these are yellow rectangles).

here is my code

import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QLineEdit, QVBoxLayout, QHBoxLayout, QSpacerItem, QSizePolicy
from PyQt5 import QtGui, QtCore
from PyQt5.QtGui import QCursor, QFont
from PyQt5.QtCore import Qt


class TransparentButton(QLabel):
    def __init__(self, name, parent=None):
        super().__init__(parent)
        self.setAlignment(Qt.AlignCenter)
        self.setStyleSheet("background-color: transparent; color : white;")
        self.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
        self.setFont(QFont('Arial', 20))
        self.name = name

        self.value = 0
        self.setText('')

    def mousePressEvent(self, event):
        # проверяем, что нажата нужная кнопка и устанавливаем значение в 20
        if self.name == "Button 1" or self.name == "Button 3" or self.name == "Button 5" or self.name == "Button 7":
            self.value = 20
        else:
            self.value = 10
        self.update_text()

    def update_text(self):
        self.setText(str(self.value))

class EditableLabel(QLineEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAlignment(Qt.AlignCenter)
        self.setStyleSheet("background-color: transparent; color : white;")
        self.setFont(QFont('Arial', 20))
        self.setPlaceholderText("")


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.init_ui()


    def init_ui(self):
    # устанавливаем фоновое изображение
        pixmap = QPixmap("theme_game.png")
        self.setFixedSize(pixmap.width(), pixmap.height()) # устанавливаем фиксированный размер окна
        self.setWindowFlags(Qt.FramelessWindowHint) # убираем рамку окна
        self.setAttribute(Qt.WA_TranslucentBackground) # устанавливаем прозрачный фон
        self.label = QLabel(self)
        self.label.setPixmap(pixmap)
        self.label.setGeometry(0, 0, pixmap.width(), pixmap.height())

        

        # создаем сетку с кнопками
        grid_buttons = QGridLayout(self)
        grid_buttons.setHorizontalSpacing(50)
        grid_buttons.setVerticalSpacing(50)
        names = ['1', 'Bck', 'Button 1', 'Close',
                'Button 3', '8', '9', '/',
                '4', '5', '6', 'Button 5',
                '1', 'Button 7', '3', '-']

        positions = [(i,j) for i in range(4) for j in range(4)]

        for position, name in zip(positions, names):

            if name == '':
                continue
            button = TransparentButton(name)
            grid_buttons.addWidget(button, *position)

        
        vertical_layout = QVBoxLayout()
        for i in range(4):
            text_edit = EditableLabel(self)
            text_edit.setMinimumSize(100, 100)
            text_edit.setMaximumSize(200, 200)
            vertical_layout.addWidget(text_edit)
        vertical_layout.addStretch(1)

        
        
        
        # добавляем вертикальную сетку в основную горизонтальную сетку
        lay = QHBoxLayout()
        lay.addLayout(grid_buttons)
        lay.addLayout(vertical_layout)

        # устанавливаем отступы вокруг сетки кнопок
        self.layout().setContentsMargins(795, 35, 70, 155)
        self.setLayout(lay)

        # перемещаем окно на нужную позицию
        self.move(500, 200)

        self.showFullScreen()


    def resizeEvent(self, event):
        pixmap = QPixmap("theme_game.png")
        self.setFixedSize(pixmap.width(), pixmap.height())
        self.label.setGeometry(0, 0, pixmap.width(), pixmap.height())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = App()
sys.exit(app.exec_())

Screenshot:

如何在PyQt5中将小部件并排安装?

I've tried every method I know. I made separate GridLayout grids, tried to add positioning to the loop, and so on. Nothing helps

This is my expectation:

如何在PyQt5中将小部件并排安装?

答案1

得分: 0

我尝试了您的代码,我看到在这一行中删除self可能会起作用:
grid_buttons = QGridLayout(self)
改为:
grid_buttons = QGridLayout()
这可能是当您将一个布局设置给两个不同的小部件时发生的错误(我以前没有尝试过这样做)。

英文:

I tried your code and I see removing self in this line might work:

grid_buttons = QGridLayout(self)

To this:

grid_buttons = QGridLayout()

This might be a bug happened when you set 1 layout to 2 different widgets (I have not try this before).

huangapple
  • 本文由 发表于 2023年6月8日 15:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定