PyQt5:将按钮位置附加到 window 坐标
PyQt5: attach button position to the window coordinates
所以,我正在尝试使用 PyQt5 进行一些试验。我创建了一个带有两个 QPushButtons
的基本 window。
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy
import sys
class Okno(QMainWindow):
def __init__(self):
super(Okno, self).__init__()
self.setGeometry(500, 500, 900, 500) # window size setGeometry(xpos, ypos, w, h)
# self.setFixedSize(900, 500) # fix window position
self.setWindowTitle("My window")
self.label = QtWidgets.QLabel(self)
self.button1 = QtWidgets.QPushButton(self)
self.button2 = QtWidgets.QPushButton(self)
self.iniUI()
# Buttons
def iniUI(self):
self.button1.setText("Open file")
self.button1.move(75, 450)
self.button1.setMinimumWidth(150)
self.button1.clicked.connect(Button_events.open_file) # add (connect) button event
self.button2.setText("Exit")
self.button2.move(750, 450)
self.button2.clicked.connect(exit)
# Button events
class Button_events(QMainWindow):
def open_file(self):
print("Open file")
# Main method
def window():
app = QApplication(sys.argv)
okno = Okno()
okno.show()
sys.exit(app.exec_())
window()
我已经通过绝对坐标设置了按钮的位置。当我调整 My Window
大小时,按钮只是保持其绝对坐标。
如何将按钮的坐标附加到 My Window
,这样当我缩放 window 时,按钮会在其中移动。
是否可以设置相对于最小 QPushButtons
大小的最小 My Window
大小(当按钮不适合 My Window
时,无法将其调整得更小) ?
这是我想要实现的简单屏幕(原始按钮被切成两半,如果 My Window
变小它们就会消失):
谢谢。
我建议使用布局来处理所有小部件的位置和 window 的最小尺寸。使用 QGridLayout,您可以将按钮对齐到左下角和右下角。
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy
import sys
class Okno(QMainWindow):
def __init__(self):
super(Okno, self).__init__()
self.setGeometry(500, 500, 900, 500)
self.setWindowTitle("My window")
self.label = QtWidgets.QLabel()
self.button1 = QtWidgets.QPushButton()
self.button2 = QtWidgets.QPushButton()
self.iniUI()
# Buttons
def iniUI(self):
w = QtWidgets.QWidget()
self.setCentralWidget(w)
grid = QtWidgets.QGridLayout(w)
self.button1.setText("Open file")
self.button1.setMinimumWidth(150)
self.button1.clicked.connect(self.open_file)
self.button2.setText("Exit")
self.button2.clicked.connect(self.close)
grid.addWidget(self.button1, 0, 0, QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom)
grid.addWidget(self.button2, 0, 1, QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
def open_file(self):
print("Open file")
def window():
app = QApplication(sys.argv)
okno = Okno()
okno.show()
sys.exit(app.exec_())
window()
所以,我正在尝试使用 PyQt5 进行一些试验。我创建了一个带有两个 QPushButtons
的基本 window。
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy
import sys
class Okno(QMainWindow):
def __init__(self):
super(Okno, self).__init__()
self.setGeometry(500, 500, 900, 500) # window size setGeometry(xpos, ypos, w, h)
# self.setFixedSize(900, 500) # fix window position
self.setWindowTitle("My window")
self.label = QtWidgets.QLabel(self)
self.button1 = QtWidgets.QPushButton(self)
self.button2 = QtWidgets.QPushButton(self)
self.iniUI()
# Buttons
def iniUI(self):
self.button1.setText("Open file")
self.button1.move(75, 450)
self.button1.setMinimumWidth(150)
self.button1.clicked.connect(Button_events.open_file) # add (connect) button event
self.button2.setText("Exit")
self.button2.move(750, 450)
self.button2.clicked.connect(exit)
# Button events
class Button_events(QMainWindow):
def open_file(self):
print("Open file")
# Main method
def window():
app = QApplication(sys.argv)
okno = Okno()
okno.show()
sys.exit(app.exec_())
window()
我已经通过绝对坐标设置了按钮的位置。当我调整 My Window
大小时,按钮只是保持其绝对坐标。
如何将按钮的坐标附加到 My Window
,这样当我缩放 window 时,按钮会在其中移动。
是否可以设置相对于最小 QPushButtons
大小的最小 My Window
大小(当按钮不适合 My Window
时,无法将其调整得更小) ?
这是我想要实现的简单屏幕(原始按钮被切成两半,如果 My Window
变小它们就会消失):
谢谢。
我建议使用布局来处理所有小部件的位置和 window 的最小尺寸。使用 QGridLayout,您可以将按钮对齐到左下角和右下角。
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy
import sys
class Okno(QMainWindow):
def __init__(self):
super(Okno, self).__init__()
self.setGeometry(500, 500, 900, 500)
self.setWindowTitle("My window")
self.label = QtWidgets.QLabel()
self.button1 = QtWidgets.QPushButton()
self.button2 = QtWidgets.QPushButton()
self.iniUI()
# Buttons
def iniUI(self):
w = QtWidgets.QWidget()
self.setCentralWidget(w)
grid = QtWidgets.QGridLayout(w)
self.button1.setText("Open file")
self.button1.setMinimumWidth(150)
self.button1.clicked.connect(self.open_file)
self.button2.setText("Exit")
self.button2.clicked.connect(self.close)
grid.addWidget(self.button1, 0, 0, QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom)
grid.addWidget(self.button2, 0, 1, QtCore.Qt.AlignRight | QtCore.Qt.AlignBottom)
def open_file(self):
print("Open file")
def window():
app = QApplication(sys.argv)
okno = Okno()
okno.show()
sys.exit(app.exec_())
window()