为什么在 PyQt5 中单击或尝试最大化窗口时最大化按钮被移除?

huangapple go评论66阅读模式
英文:

Why maximize button get removed when I click or try to maximize window in PyQt5

问题

这是您提供的Python代码,用于创建PyQt应用程序中的UI界面。问题似乎与窗口最大化按钮的设置有关。以下是代码的一部分,其中包括与窗口标志有关的设置:

self.setWindowFlags(self.windowFlags() | Qt.WindowMaximizeButtonHint)
self.setWindowFlags(self.windowFlags() | Qt.WindowCloseButtonHint)
self.setWindowFlags(self.windowFlags() | Qt.WindowMinimizeButtonHint)

这些行代码似乎会多次设置窗口标志,可能导致问题。您可以尝试将它们合并为一个单独的设置:

self.setWindowFlags(self.windowFlags() | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)

这可能会解决窗口最大化的问题。如果问题仍然存在,请提供更多信息,以便进一步帮助您解决问题。

英文:

I've a simple pyqt application in which I've some code for UI which is responsible for realtime speech-to-text in application everything works fine but when I try to maximize my application by clicking on maximize icon it get remove and window does not maximized here is output how it looks

为什么在 PyQt5 中单击或尝试最大化窗口时最大化按钮被移除?

This is my code.

class MainWindow(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs) -> None:
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setWindowTitle("Speech To Text")
        self.setWindowFlags(self.windowFlags() | Qt.WindowMaximizeButtonHint)
        self.setWindowFlags(self.windowFlags() | Qt.WindowCloseButtonHint)
        self.setWindowFlags(self.windowFlags() | Qt.WindowMinimizeButtonHint)

        self.app_name = QtWidgets.QLabel("Speech To Text")

        self.hbox = QtWidgets.QHBoxLayout()
        self.someButton1 = QtWidgets.QPushButton("btn1")
        self.someButton2 = QtWidgets.QPushButton("btn2")
        self.hbox.addWidget(self.someButton1)
        self.hbox.addWidget(self.someButton2)

        self.grid = QtWidgets.QGridLayout()
        self.vbox = QtWidgets.QVBoxLayout()
        
        self.topBtn1 = QtWidgets.QPushButton("Top Button 1")
        self.topBtn2 = QtWidgets.QPushButton("Top Button 2")
        self.topLayout = QtWidgets.QVBoxLayout()
        self.topLayout.addWidget(self.topBtn1)
        self.topLayout.addWidget(self.topBtn2)

        self.startButton = QtWidgets.QPushButton()
        self.startLabel = QtWidgets.QLabel()
        self.startLabel.setText("Tap To Record")
        self.startLayout = QtWidgets.QVBoxLayout()
        self.startLayout.addWidget(self.startButton, alignment=Qt.AlignCenter)
        self.startLayout.addWidget(self.startLabel, alignment=Qt.AlignCenter)        
        self.startLayout.addStretch()

        self.stopButton = QtWidgets.QPushButton()
        self.stopLabel = QtWidgets.QLabel()
        self.stopLabel.setText("Stop")
        self.stopLayout = QtWidgets.QHBoxLayout()
        self.stopLayout.addWidget(self.stopButton)
        self.stopLayout.addWidget(self.stopLabel)

        self.vbox.addLayout(self.topLayout)
        self.vbox.addLayout(self.startLayout)
        self.vbox.addLayout(self.stopLayout)
        self.vbox.addWidget(self.app_name, alignment=Qt.AlignCenter, stretch=1)
        
        self.editor = QtWidgets.QTextEdit()

        self.grid.addLayout(self.hbox, 0, 2)
        self.grid.addLayout(self.vbox, 1, 1)
        self.grid.addWidget(self.editor, 1, 2)
        self.setLayout(self.grid)

I've not provided full code, only main code, and also I'm using Ubuntu, and some methods on the MainWindow class update the UI on clicking buttons (it is just for extra information). What's the issue please let me know because on same system another simple application works perfectly.

答案1

得分: 0

问题是由于在Windows和Linux上的间距差异导致的,我发现了这一点。我在QVBoxLayout()元素上添加了太多的边距,导致它们溢出了屏幕。在减少边距并设置window.showMaximized()后,它现在占据了整个屏幕的宽度和高度,并且现在正常工作。

请确保在不同的操作系统上测试间距,例如Windows、Linux和MacOS。在我的情况下,Windows 需要150点的边距才能得到正确的结果,但在Linux上溢出了,所以我将它设置为50点,问题解决了!

英文:

I figured out that the problem was different on Windows and Linux due to differences in spacing. I had added too much margin to the QVBoxLayout() elements, causing them to overflow the screen. After reducing the margins and setting window.showMaximized(), it now takes up the full screen width and height, and it's working now.

Make sure to test the spacing on different operating systems, such as Windows, Linux, and MacOS. In my case, Windows took a 150-point margin to give me the correct result, but in Linux it overflowed, so I set it to 50-point and it fixed!

huangapple
  • 本文由 发表于 2023年4月19日 15:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定