python pyqt5 窗体自适应_PyQt5固定窗口大小

这篇博客介绍了如何在PyQt5中创建一个名为`MyFirstGUI`的对话框,设置了固定大小,并在初始化时加载。博客详细展示了如何定义布局,添加QLabel和QDialogButtonBox,以及连接按钮的接受和拒绝信号。示例代码演示了如何组织这些元素,以创建一个具有'OK'和'Cancel'按钮的简单用户界面。
摘要由CSDN通过智能技术生成

分别在加载用户界面(如果使用.UI文件)后或在窗口的init中加载。应该是这样的:class MyDialog(QtWidgets.QDialog):

def __init__(self):

super(MyDialog, self).__init__()

self.setFixedSize(640, 480)

如果这对你有用,请告诉我。

编辑:这是所提供的代码应如何重新格式化以工作的方式。from PyQt5 import QtWidgets

# It is considered a good tone to name classes in CamelCase.

class MyFirstGUI(QtWidgets.QDialog):

def __init__(self):

# Initializing QDialog and locking the size at a certain value

super(MyFirstGUI, self).__init__()

self.setFixedSize(411, 247)

# Defining our widgets and main layout

self.layout = QtWidgets.QVBoxLayout(self)

self.label = QtWidgets.QLabel("Hello, world!", self)

self.buttonBox = QtWidgets.QDialogButtonBox(self)

self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)

# Appending our widgets to the layout

self.layout.addWidget(self.label)

self.layout.addWidget(self.buttonBox)

# Connecting our 'OK' and 'Cancel' buttons to the corresponding return codes

self.buttonBox.accepted.connect(self.accept)

self.buttonBox.rejected.connect(self.reject)

if __name__ == '__main__':

import sys

app = QtWidgets.QApplication(sys.argv)

gui = MyFirstGUI()

gui.show()

sys.exit(app.exec_())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值