Расположение элементов PyQT5
Всем привет. Я новичек в pyqt5 и пишу свою игру. Проблема заключается в расположении элементов в окне. С QVBoxLayout и QHBoxLayout я разобрался, но вот с QGridLayout большие проблемы, несмотря на то, что прошерстил много статей. Подскажите, как создается сетка в этом QGridLayout, чтобы можно было располагать элементы в любое место.
Например, подскажите как разместить кнопки ("Вкл", "Выкл", которые находятся в верхней части окна) в место, где они нарисованы (в левой части окна).

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel,
QVBoxLayout, QGridLayout, QRadioButton, QLineEdit, QHBoxLayout)
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import Qt
import pygame
from create import Create
class Program(QWidget):
def __init__(self):
super().__init__()
self.setObjectName("mainwindow")
self.widget = QWidget(self)
self.widget.setObjectName("widget")
self.layout = QGridLayout(self.widget)
self.play_music()
# self.setStyleSheet("background-image: url(images/Frame 1.png);")
# self.pixam = QPixmap('images/Rectangle 9.png')
# self.label.setPixmap(self.pixam)
# self.resize(1000, 561)
self.menu()
# менюшка
def menu(self):
self.setStyleSheet('''
#widget {
border-image: url("images/Frame 1.png") 0 0 0 0;
}
#label {
color: #fff;
}
''')
self.label = QLabel(alignment=Qt.AlignCenter)
self.label.setObjectName("label")
self.label1 = QLabel("")
self.label2 = QLabel("")
self.label3 = QLabel("")
self.label4 = QLabel("")
self.label5 = QLabel("")
self.label6 = QLabel("")
self.label7 = QLabel("")
self.label8 = QLabel("")
self.label9 = QLabel("")
self.button_create = QPushButton('Создать')
self.button_create.setObjectName("button_create")
self.button_create.clicked.connect(lambda: [
self.clean_data([
self.button_create,
self.button_settings,
self.button_about,
self.label1,
self.label2,
self.label3,
self.label4,
self.label5,
self.label6,
self.label7,
self.label8,
self.label9,
]),
self.create()
])
self.layout.addWidget(self.button_create, 5, 1, 1, 1)
self.button_settings = QPushButton('Настройки')
self.button_settings.setObjectName("button_settings")
self.button_settings.clicked.connect(lambda: [
self.clean_data([
self.button_create,
self.button_settings,
self.button_about,
self.label1,
self.label2,
self.label3,
self.label4,
self.label5,
self.label6,
self.label7,
self.label8,
self.label9,
]),
self.settings()
])
self.layout.addWidget(self.button_settings, 7, 1, 1, 1)
self.button_about = QPushButton("О программе")
self.button_about.setObjectName("button_about")
self.button_about.clicked.connect(lambda: [
self.clean_data([
self.button_create,
self.button_settings,
self.button_about,
self.label1,
self.label2,
self.label3,
self.label4,
self.label5,
self.label6,
self.label7,
self.label8,
self.label9,
]),
self.about_program()
])
self.layout.addWidget(self.button_about, 9, 1, 1, 1)
self.layout.addWidget(self.label1, 1, 1, 1, 1)
self.layout.addWidget(self.label2, 2, 1, 1, 1)
self.layout.addWidget(self.label3, 3, 1, 1, 1)
self.layout.addWidget(self.label4, 4, 1, 1, 1)
self.layout.addWidget(self.label5, 6, 1, 1, 1)
self.layout.addWidget(self.label6, 8, 1, 1, 1)
self.layout.addWidget(self.label7, 10, 1, 1, 1)
self.layout.addWidget(self.label8, 11, 1, 1, 1)
self.layout.addWidget(self.label9, 0, 1, 1, 1)
main_layout = QVBoxLayout(self)
main_layout.addWidget(self.widget)
# вкладка создания квадрата
def create(self):
self.setStyleSheet('''
#widget {
border-image: url("images/Frame 2.png") 0 0 0 0;
}
#label {
color: #fff;
}
''')
self.field = QLineEdit()
self.field.setObjectName("field")
self.field.setFixedWidth(200)
self.field.setFixedHeight(30)
self.layout.addWidget(self.field, 1, 2, 1, 1)
self.create_button = QPushButton("Создать")
self.create_button.setObjectName("create_button")
self.create_button.clicked.connect(lambda: [
self.clean_data([
self.field,
self.create_button,
self.back_button,
]),
self.create_square(),
])
self.layout.addWidget(self.create_button, 2, 2, 1, 1)
self.back_button = QPushButton("Назад")
self.back_button.setObjectName("back_button")
self.back_button.clicked.connect(lambda: [
self.clean_data([
self.field,
self.create_button,
self.back_button,
]),
self.menu()
])
self.layout.addWidget(self.back_button, 3, 2, 1, 1)
# вкладка настройки
def settings(self):
self.vbox = QVBoxLayout()
self.vbox.addStretch(1)
self.hbox = QHBoxLayout()
# self.hbox.addStretch(1)
self.setStyleSheet('''
#widget {
border-image: url("images/Frame 4.png") 0 0 0 0;
}
#label {
color: #fff;
}
''')
self.rad_music_play = QRadioButton("Вкл")
self.rad_music_play.setChecked( True )
self.rad_music_play.toggled.connect(lambda: [self.play_music()])
self.hbox.addWidget(self.rad_music_play)
self.rad_music_off = QRadioButton("Выкл")
self.rad_music_off.toggled.connect(lambda: [pygame.quit()])
self.hbox.addWidget(self.rad_music_off)
self.back_button = QPushButton('Назад')
self.back_button.setObjectName("back_button")
self.back_button.clicked.connect(lambda: [
self.clean_data([
self.rad_music_play,
self.rad_music_off,
self.back_button,
]),
self.menu()
])
self.vbox.addWidget(self.back_button)
self.layout.addLayout(self.hbox, 1, 0)
self.layout.addLayout(self.vbox, 2, 0)
# вкладка о программе
def about_program(self):
# self.setObjectName("mainwindow2")
# self.initUI('images/mulyadi-TyjFLWdZris-unsplash 1.png')
self.setStyleSheet('''
#widget {
border-image: url("images/mulyadi-TyjFLWdZris-unsplash 1.png") 0 0 0 0;
}
#label {
color: #fff;
}
''')
self.label.setText(
'В одном из приведённых ниже слов допущена ошибка в \nпост'
'ановке ударения: НЕВЕРНО выделена буква, \nобозначающая '
'ударный гласный звук. Выпишите это слово.')
self.layout.addWidget(self.label)
self.back_button = QPushButton("Назад")
self.back_button.setObjectName("back_button")
self.back_button.clicked.connect(lambda: [
self.clean_data([
self.label,
self.back_button,
]),
self.menu()
])
self.layout.addWidget(self.back_button, 2, 1)
# создания магического квадрата
def create_square(self):
# первое создание квадрата
try:
self.value = self.field.text()
self.square = Create(int(self.value))
result = self.square.generateSquare()
self.draw_square(int(self.value), result)
# пересоздаем квадрат
except:
result = self.square.generateSquare()
self.draw_square(int(self.value), result)
# вывод квадрата с кнопкой пересоздания
def draw_square(self, n, *args):
self.setStyleSheet('''
#widget {
border-image: url("images/Frame 3.png") 0 0 0 0;
}
#label {
color: #fff;
}
''')
print(*args)
# удаление мусора
def clean_data(self, *args):
for arg in args:
for object_name in arg:
object_name.deleteLater()
# запуск музыки
def play_music(self):
pygame.init()
self.song = pygame.mixer.Sound('images/Смешарики.mp3')
self.song.play()
# def initUI(self, path):
# self.setStyleSheet("#mainwindow{border-image:url("f'{path}'")}")
QSS = '''
#mainwindow {
background-color: #a1193d;
}
#mainwindow2 {
background-color: #105652;
}
#label {
color: #fff;
font-size: 24px;
}
#field {
border: 2px solid gray;
border-radius: 10px;
padding: 0 8px;
}
QPushButton {
background-image : url(images/Rectangle 9.png);
border-style: outset;
border-radius: 10px;
border-color: beige;
font: bold 14px;
min-width: 10em;
padding: 6px;
min-width: 280px;
max-width: 280px;
min-height: 35px;
max-height: 35px;
}
QPushButton:hover {
background-color: #565612;
}
QRadioButton::indicator {
width: 25px;
height: 25px;
}
#widget {
border-image: url("images/Frame 1.png") 0 0 0 0;
}
'''
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(QSS)
ex = Program()
ex.resize(1000, 561)
ex.setWindowTitle('Icon')
ex.setWindowIcon(QIcon('im.png'))
ex.show()
sys.exit(app.exec_())
Заранее спасибо за вашу помощь.