在Ubuntu 18.04上使用系统d服务启动时的PyGame图形用户界面

t98cgbkg  于 2022-09-19  发布在  Linux
关注(0)|答案(1)|浏览(151)

我正尝试在Linux系统上运行一个带有系统d服务的启动时的PyGame脚本。

以下是我的服务文件:test.service

[Unit]
Description=Test
Wants=systemd-logind.service systemd-user-sessions.service display-manager.service
After=systemd-logind.service systemd-user-sessions.service display-manager.service

[Service]
ExecStart=/usr/bin/python3 /home/enws/Desktop/test/main.py

[Install]
WantedBy=graphical.target

当我尝试使用sudo systemctl start test启动服务时,什么都没有发生,并且我看到了使用sudo systemctl status test的日志,我得到:

● test.service - Test
   Loaded: loaded (/etc/systemd/system/test.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2022-09-15 16:03:31 +03; 586ms ago
  Process: 26454 ExecStart=/usr/bin/python3 /home/enws/Desktop/test/main.py (code=exited, status=1/FAILURE)
 Main PID: 26454 (code=exited, status=1/FAILURE)

Eyl 15 16:03:30 dasdasdasdasd systemd[1]: Started Test.
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: pygame 2.1.2 (SDL 2.0.16, Python 3.6.9)
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: Hello from the pygame community. https://www.pygame.org/contribute.html
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: Traceback (most recent call last):
Eyl 15 16:03:31 dasdasdasdasd python3[26454]:   File "/home/enws/Desktop/test/main.py", line 8, in <module>
Eyl 15 16:03:31 dasdasdasdasd python3[26454]:     window_surface = pygame.display.set_mode((0, 0))
Eyl 15 16:03:31 dasdasdasdasd python3[26454]: pygame.error: No available video device
Eyl 15 16:03:31 dasdasdasdasd systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
Eyl 15 16:03:31 dasdasdasdasd systemd[1]: test.service: Failed with result 'exit-code'.

我的main.py来自pyGame_gui文档:

import pygame
import pygame_gui

pygame.init()

pygame.display.set_caption('Quick Start')
window_surface = pygame.display.set_mode((0, 0))

background = pygame.Surface((800, 600))
background.fill(pygame.Color('#000000'))

manager = pygame_gui.UIManager((800, 600))

hello_button = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((350, 275), (100, 50)),
                                            text='Say Hello',
                                            manager=manager)

clock = pygame.time.Clock()
is_running = True

while is_running:
    time_delta = clock.tick(60)/1000.0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False

        if event.type == pygame_gui.UI_BUTTON_PRESSED:
            if event.ui_element == hello_button:
                print('Hello World!')

        manager.process_events(event)

    manager.update(time_delta)

    window_surface.blit(background, (0, 0))
    manager.draw_ui(window_surface)

    pygame.display.update()
jdzmm42g

jdzmm42g1#

相反,这起作用了

sudo nano /home/enws/.config/autostart/*.desktop

[Desktop Entry]
Name=MyApp
Type=Application
Exec=python3 /home/enws/Desktop/test/main.py
Terminal=false

相关问题