简体   繁体   中英

how to make a window not moveable in pyqt5

I want to "lock" my interface so a user can not move the window from its position how can I do that?

I only found so far the QDockWidg class which can be set to setFloating(False)

but how can I do that with my window below?

import sys

import os
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg



class Example(qtw.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # your code will go here


        vlayout = qtw.QVBoxLayout()
        hlayout = qtw.QHBoxLayout()

        heading = qtw.QLabel("wählen")
        self.test_button1 = qtw.QRadioButton("test1")
        self.test_button2 = qtw.QRadioButton("test2")

        self.select_button = qtw.QPushButton("select")

        hlayout.addWidget(self.test_button1)
        hlayout.addWidget(self.test_button1)


        vlayout.addWidget(heading)
        vlayout.addLayout(hlayout)
        vlayout.addWidget(self.select_button)

        self.setLayout(vlayout)

        self.show()



if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = Example()

    w.show()
    sys.exit(app.exec_())

For example, connect the signal toggled to self.test_button1 .

[signal]void QAbstractButton::toggled(bool checked)

This signal is emitted whenever a checkable button changes its state. checked is true if the button is checked, or false if the button is unchecked.

In the on_off_func slot, check the status of self.test_button1 and set the desired Flag . After that, be sure to call self.show () .

import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg



class Example(qtw.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # your code will go here


        vlayout = qtw.QVBoxLayout()
        hlayout = qtw.QHBoxLayout()

        heading = qtw.QLabel("wählen")
        self.test_button1 = qtw.QRadioButton("test1")
        self.test_button1.toggled.connect(self.on_off_func)          # +
        self.test_button2 = qtw.QRadioButton("test2")

        self.select_button = qtw.QPushButton("select")

        hlayout.addWidget(self.test_button1)
        hlayout.addWidget(self.test_button2)

        vlayout.addWidget(heading)
        vlayout.addLayout(hlayout)
        vlayout.addWidget(self.select_button)

        self.setLayout(vlayout)

        self.show()

    def on_off_func(self, checked):
        if checked:
            self.setWindowFlags(qtc.Qt.FramelessWindowHint)
        else:
            self.setWindowFlags(qtc.Qt.Window)
        self.show()

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    w = Example()
    w.show()
    sys.exit(app.exec_())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM