如何使用 qml 为 xfce 制作锁屏(无需 sddm 锁屏)

如何使用 qml 为 xfce 制作锁屏(无需 sddm 锁屏)

我正在尝试使用 QML 和 python PyQt5 为 Xfce 和 gnome 制作锁屏。

主文件

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0

ApplicationWindow {
    property var enter: 0
    id: window
    objectName: "popup"
    width: Screen.width
    height: Screen.height
    color: "#b3000000"
    visible: true
    visibility: Window.FullScreen
    flags: Qt.WindowStaysOnTopHint
    Timer{
        signal entered
        objectName: 'timer'
        repeat: true
        running: true
        interval: 10
        onTriggered: {
            if(enter === 1 ){
                enter = 0
                entered()
            }
        }
    }

    Item {
        id: element1
        x: 474
        y: 302
        width: 418
        height: 328
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter

        RoundButton {
            id: roundButton
            clip: true
            anchors.top: parent.top
            anchors.topMargin: 72
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 106
            anchors.left: parent.left
            anchors.leftMargin: 134
            anchors.right: parent.right
            anchors.rightMargin: 134
            background: Rectangle{
                anchors.fill: parent
                color: "white"
                radius: parent.radius
            }

            Image {
                id: image
                anchors.fill: parent
                objectName: 'image'
                fillMode: Image.PreserveAspectCrop
                source: "qrc:/qtquickplugin/images/template_image.png"
                layer.enabled: true
                layer.effect: OpacityMask {
                    maskSource: parent
                }
            }
        }

        Text {
            objectName: 'text'
            id: element2
            color: "#f9f9f9"
            text: qsTr("User Name")
            anchors.top: parent.top
            anchors.topMargin: 6
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 275
            anchors.left: parent.left
            anchors.leftMargin: 170
            anchors.right: parent.right
            anchors.rightMargin: 170
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 24
        }

        Item {
            id: element
            anchors.top: parent.top
            anchors.topMargin: 247
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 18
            anchors.left: parent.left
            anchors.leftMargin: 0
            anchors.right: parent.right
            anchors.rightMargin: 0

            Rectangle {
                id: rectangle
                color: "#ffffff"
                radius: 21
                anchors.top: parent.top
                anchors.topMargin: 11
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                anchors.left: parent.left
                anchors.leftMargin: 8
                anchors.right: parent.right
                anchors.rightMargin: 118
                clip: true

                TextInput {
                    id: textInput
                    anchors.fill: parent
                    cursorVisible: true
                    clip: true
                    focus: true
                    Keys.onPressed: {
                        if (event.key === Qt.Key_Return){
                            enter = 1
                        }
                        if (event.key === Qt.Key_){
                            console.log('anythong')
                        }
                    }
                    objectName: 'input'
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: 12
                }
            }

            Button {
                id: button1
                objectName: 'button'
                text: qsTr("Button")
                anchors.top: parent.top
                anchors.topMargin: 12
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 11
                anchors.left: parent.left
                anchors.leftMargin: 308
                anchors.right: parent.right
                anchors.rightMargin: 10
            }
        }

    }
}

主程序

import sys
from PyQt5 import QtCore, QtGui, QtQml
from PyQt5.QtCore import QObject, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtQml import QQmlApplicationEngine
from threading import Thread
import os
import importlib
import subprocess
import tempfile
import re
import random
import os.path
from os import path
dir_path = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
    myApp = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    context = engine.rootContext()
    context.setContextProperty("main", engine)
    engine.load('main.qml')
    win = engine.rootObjects()[0]
    button = win.findChild(QObject, "button")
    password = win.findChild(QObject, 'input')
    text = win.findChild(QObject, 'text')
    timer = win.findChild(QObject, 'timer')
    image = win.findChild(QObject, 'image')
    def imgFunction():
        a  = os.popen('echo $USER').readlines()
        def convert(a):
            new = ""
            for x in a:
                new += x
            return new
        str1 = convert(a)
        username = str1.replace('\n','')
        with open('/var/lib/AccountsService/users/'+username, 'r') as file:
            raw = file.readlines()
            for line in raw :
                if re.search(r'^Icon=', line):
                    imgLocation = line.replace('Icon=', "")
                    imgLocation = imgLocation.replace(' ', '')
                    imgLocation = imgLocation.replace('\n', '')
                    url = QtCore.QUrl.fromLocalFile(os.path.join(dir_path, imgLocation))
                    if str(path.exists(imgLocation)) == 'True':
                        image.setProperty('source', url)
                    else:
                        image.setProperty('source', 'windows-10-user-account-login-icon.png')
    def printFunction():
        a = password.property('text')
        f= open("bash","w+")
        f.write("#! /bin/bash\nPASSWD='"+a+"'\nsudo -k\nif sudo -lS &> /dev/null << EOF\n$PASSWD\nEOF\nthen\n    echo 'Correct password.'\nelse \n    echo 'Wrong password.'\nfi")
        f.close()
        a  = os.popen('bash ./bash').readlines()
        def convert(a):
            new = ""
            for x in a:
                new += x
            return new
        str1 = convert(a)
        check = str1.replace('\n','')
        if 'Correct password.' in check:
            win.close()
        else:
            text.setProperty('text', 'Wrong')
            password.setProperty('text', "")
    imgFunction()
    button.clicked.connect(printFunction)
    timer.entered.connect(printFunction)
    win.showFullScreen()
    sys.exit(myApp.exec_())

我遇到了很多问题,在运行时如果不输入密码就会破坏锁屏。键盘快捷键很容易破坏锁屏。我试过Qt.Popup标记,但它不适用于 TextInput(可能是错误)。如何摆脱这些键盘快捷键。

如果我做错了,您会建议我提供哪份指南来针对 ubuntu 进行锁屏开发。

相关内容