制作一个可以调整到屏幕的pygame可调整大小的窗口 - python

我用以下方法创建了一个简单的屏幕:

user_display = pg.display.Info()

win = pg.display.set_mode((user_display.current_w, user_display.current_h), pg.RESIZABLE)

这将创建一个可以调整大小的窗口,该窗口会将其自身设置为用户当前的屏幕尺寸。问题在于窗口的位置偏向角落。理想情况下,默认情况下,单击窗口顶部的全屏图标时会发生所需的效果。有没有一种方法可以将其设置为使屏幕自动捕捉到顶部?

参考方案

我猜您在谈论最大化窗口,而不是全屏模式。

如果只关心Windows,则可以使用Win32-API最大化窗口(使用win32gui模块):

import pygame
import win32gui
import win32con

def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
    win32gui.ShowWindow(win32gui.GetForegroundWindow(), win32con.SW_MAXIMIZE)

    ball = pygame.Rect(0, 300, 32, 32)
    dir = 1

    while True:
        events = pygame.event.get()

        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode((e.w,e.h), pygame.RESIZABLE)
                ball = pygame.Rect(0, int(e.h/2), 32, 32)

        if not screen.get_rect().contains(ball):
            dir *= -1
        ball.move_ip(dir, 0)

        screen.fill((30, 30, 30))
        pygame.draw.rect(screen, pygame.Color("dodgerblue"), ball)
        pygame.display.flip()
main()

如果您想最大化其他系统(例如Linux)上的窗口,我不知道,但是您可以将pygame窗口包装在tkinter窗口中并最大化该窗口:

import pygame
import tkinter as tk
import os

def main():
    root = tk.Tk()
    embed = tk.Frame(root, width = 800, height = 600)
    embed.pack(fill="both", expand=True)
    os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
    os.environ['SDL_VIDEODRIVER'] = 'windib'
    root.state('zoomed')
    screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)

    ball = pygame.Rect(0, 300, 32, 32)
    dir = 1

    while True:
        events = pygame.event.get()

        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode((e.w,e.h), pygame.RESIZABLE)
                ball = pygame.Rect(0, int(e.h/2), 32, 32)

        if not screen.get_rect().contains(ball):
            dir *= -1
        ball.move_ip(dir, 0)

        screen.fill((30, 30, 30))
        pygame.draw.rect(screen, pygame.Color("dodgerblue"), ball)
        pygame.display.flip()
        root.update()      
main()

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python:图像处理可产生皱纹纸效果 - python

也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python:无法识别Pip命令 - python

这是我拍摄的屏幕截图。当我尝试在命令提示符下使用pip时,出现以下错误消息:pip无法识别为内部或外部命令,可操作程序或批处理文件。我已经检查了这个线程:How do I install pip on Windows?我所能找到的就是我必须将"C:\PythonX\Scripts"添加到我的类路径中,其中X代表python版本。如您在我的…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…