简体   繁体   English

Python 脚本不在 Ubuntu 上运行

[英]Python script doesn't run on Ubuntu

I have coded a Python 3 script on my Windows PC and want to have it on my Ubuntu PC as well.我在我的 Windows PC 上编写了一个 Python 3 脚本,我也想在我的 Ubuntu PC 上安装它。 This script creates a PyQt5 Borderless window with Clickthrough enabled.此脚本创建一个 PyQt5 无边界 window 启用点击。 The window should be transparent aside from a label that contains a single image, labeled GreenRGB.png. window 应该是透明的,除了 label 包含一个图像,标记为 GreenRGB.png。 However, the PyQt5 Window doesn't show up.但是,PyQt5 Window 没有出现。 Why, and how can I fix this, if you please?为什么,如果你愿意,我该如何解决这个问题?

My Code我的代码

import sys

from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import *


class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.acceptDrops()
        # set the title
        self.setWindowTitle("RGB Corners")

        # creating label
        self.label = QLabel(self)

        # loading image
        self.pixmap = QPixmap('greenRGB.png')

        # adding image to label
        self.label.setPixmap(self.pixmap)

        # Optional, resize label to image size
        self.label.resize(self.pixmap.width(), self.pixmap.height())
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.setWindowFlags(QtCore.Qt.Tool)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setAttribute(Qt.WA_TransparentForMouseEvents, True)
        self.setAttribute(Qt.WA_NoChildEventsForParent, True)
        self.setWindowFlags(Qt.Window|Qt.X11BypassWindowManagerHint|Qt.WindowStaysOnTopHint|Qt.FramelessWindowHint|QtCore.Qt.Tool)

        # show all the widgets
        self.show()
        self.showMaximized()


App = QApplication(sys.argv)
window = Window()
windows = QtWidgets.QWidget()

trayIcon = QSystemTrayIcon(QIcon('test.png'), parent=App)
trayIcon.setToolTip('RGB Corners')
trayIcon.show()
trayMenu = QMenu()
exitAction = trayMenu.addAction('Exit')
exitAction.triggered.connect(App.quit)
trayIcon.setContextMenu(trayMenu)

sys.exit(App.exec())

PyQt5.12.8 is the version that I have installed. PyQt5.12.8 是我安装的版本。 After further inspection, the window is created, and apparently shown.进一步检查后,创建了 window,并且显然显示出来了。 No bugs in PyCharm, and the program does not crash. PyCharm没有bug,程序没有崩溃。 I suppose that Ubuntu could not be drawing these correctly.我想 Ubuntu 无法正确绘制这些。 The theme that I have installed has no troubles, as the default Yaru themes have the same issue.我安装的主题没有问题,因为默认的Yaru主题有同样的问题。 After further inspection, the line that caused it not to show is this: self.setWindowFlags(QtCore.Qt.Tool) .进一步检查后,导致它不显示的行是: self.setWindowFlags(QtCore.Qt.Tool)

I am so sorry for all of your troubles.我很抱歉给你带来的麻烦。 The bug was in the line self.setWindowFlags(QtCore.Qt.Tool) I do not know why this broke it, but I commented it (will delete it) and it got fixed.该错误在self.setWindowFlags(QtCore.Qt.Tool)行中 我不知道为什么会破坏它,但我评论了它(将删除它)并且它得到了修复。 However, upon doing this, the label will not resize properly, and is always too small.但是,这样做后,label 将无法正确调整大小,而且总是太小。 I guess that is for another question though.我想那是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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