[英]PyQt5: attach button position to the window coordinates
So, I'm trying to experiment a bit with PyQt5.所以,我正在尝试使用 PyQt5 进行一些实验。 I have created a basic window with two
QPushButtons
.我创建了一个带有两个
QPushButtons
的基本窗口。
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()
I have set button's position by absolute coordinates.我已经通过绝对坐标设置了按钮的位置。 When I resize
My Window
, the button simply holds its absolute coordinates.当我调整
My Window
大小时,按钮只是保持其绝对坐标。
How can I attach the button's coordinates to My Window
, so when I scale the window, the button would move within.如何将按钮的坐标附加到
My Window
,因此当我缩放窗口时,按钮会在其中移动。
Is it possible to set a minimum My Window
size with respect to minimum QPushButtons
size (when buttons won't fit My Window
, it can't be adjusted any smaller)?是否可以相对于最小
QPushButtons
大小设置最小My Window
大小(当按钮不适合My Window
,无法将其调整得更小)?
Here is a simple screen of what I'm trying to achieve (the original buttons are cut in half, they would disappear if My Window
gets any smaller):这是我想要实现的简单屏幕(原始按钮被切成两半,如果
My Window
变小,它们就会消失):
Thank you.谢谢你。
I would recommend using a layout to handle the position of all the widgets and minimum size of the window.我建议使用布局来处理所有小部件的位置和窗口的最小尺寸。 With QGridLayout, you can align the buttons to the bottom left and bottom right.
使用 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.