【问题标题】:Cursor flashing with Tkinter and Pygame integrated in one windowTkinter 和 Pygame 集成在一个窗口中的光标闪烁
【发布时间】:2019-01-16 19:20:00
【问题描述】:

下面是我正在测试的一些代码,用于将 Tkinter 和 Pygame 集成在一起。我已经设法在 Tkinter 窗口中显示了一个 pygame 显示,并且还在这个 pygame 显示的顶部显示了一个按钮。该按钮仅绘制一个白色圆圈。当程序启动时,由于没有绘制 Tkinter 的配置光标 (X_cursor),问题立即开始。然后,当我将鼠标悬停在按钮上并移开它时,光标开始闪烁到 pygame 的默认光标,然后又回到 Tkinter 的配置光标。此外,只有在鼠标处于移动状态时,光标才会恢复为 pygame 的默认值。否则,它就是正确的“X_Cursor”。

我很难理解 Tkinter 的系统,但我确信解决方案就在我面前。我只是需要一点帮助来解决这个问题。

import pygame
import pygame.key
from pygame.locals import *
import tkinter as tk
from tkinter import *
import os


#colors#
BLACK = (0,0,0)
WHITE = (255, 255, 255)
GREEN = (0, 255,0)
RED = (255, 0,0)
BLUE = (0,0, 255)

#buttons#
mButton1 = (1, 0, 0)
mButton2 = (0, 1, 0)
mButton3 = (0, 0, 1)



root = tk.Tk()
root.attributes('-fullscreen', True)
root.title("This title isn't visible since it's fullscreen")
root.config(cursor = "X_cursor")

embed = tk.Frame(root, width = 1920, height = 1080) #creates embed frame for pygame window
embed.grid(columnspan = 10, rowspan = 10) # Adds grid



os.environ['SDL_WINDOWID'] = str(embed.winfo_id())


worldWindow = pygame.display.set_mode((0,0), RESIZABLE)
worldWindow.fill(BLACK)


def draw():
    pygame.draw.circle(worldWindow, WHITE, (250,250), 125)

IMAGEOBJECT = PhotoImage( file = 'TESTIMAGE.gif')


buttonwin = tk.Frame(root, width = 75, height = 75)
buttonwin.grid(row =8, column = 8)
button1 = Button(root, image = IMAGEOBJECT,text = "Draw a circle", cursor = "circle",  command=draw)
button1.grid(row =8 , column = 8)



pygame.display.init()

#loop until user clicks close button
done = False

clock = pygame.time.Clock()

#~~~~~~~~MAIN LOOP~~~~~~~~#
while not done:
    for event in pygame.event.get():
        if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and pygame.K_ESCAPE):
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:  
            mButton = pygame.mouse.get_pressed()
            if mButton == mButton1:
                pos = pygame.mouse.get_pos()
                print(pos)

        else:
            pygame.event.clear()

    #limit to 60 frames per second
    clock.tick(60)

    #update the screen with all the draws
    pygame.display.update()
    root.update()
pygame.quit()

【问题讨论】:

  • 这听起来是一个非常困难的方法,你有具体的原因吗?
  • pygame和tkinter一起使用的原因?为我即将制作的游戏提供现成的 GUI 工具,真的。
  • 也许pygame libraries 之一比尝试将 pygame 与 tk 集成更容易使用。
  • 我建议在这种情况下使用 PGU "Phil's pyGame Utilities" 而不是 tk。提供一组标准的 GUI 小部件,可以集成到 PyGame 中。
  • 删除from tkinter import *。您不需要两次导入 tkinter。做import tkinter as tk 是首选方法,因为它有助于防止导入的方法/变量过冬。这一切都说我无法重现您的“闪烁”问题。白色 X 光标对我来说是实心的。

标签: python tkinter pygame


【解决方案1】:

这似乎是 tkinter 和 pygame 为谁显示光标而“战斗”。我将此添加到 pygame 事件循环中进行检查。

elif event.type == pygame.MOUSEMOTION:
        pygame.mouse.set_cursor(*pygame.cursors.diamond)

行为保持不变,现在在移动时显示菱形光标,在停止时显示“X”光标。当我注释掉 tkinter 光标配置时,我会在运动停止时获得 Windows 操作系统默认光标。

我认为最简单的解决方案是将 tkinter 'X' 加载到 pygame 光标编译器中,并将其用作默认值或在鼠标运动事件下使用(链接中的详细信息)。

https://www.pygame.org/docs/ref/cursors.html

我有点喜欢它的外观的另一个选项是在鼠标移动 elif 中使用此光标。

*pygame.cursors.broken_x

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2019-10-17
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多