【发布时间】:2019-09-09 08:07:43
【问题描述】:
所以我在 pygame 中制作游戏,我也想使用 tkinter。我将 pygame 窗口嵌入到 tkinter 窗口中,但我似乎无法用它做任何事情。
关于上下文,这里是完整的代码:
import Tkinter as tk
import os
import platform
import pygame
class window(object):
def __init__(self):
self.root = tk.Tk() # Main window
self.root.title("SquareScape")
self.root.iconbitmap(r'C:\Users\17_es\PycharmProjects\square_puzzle\images\icon.ico')
self.root.configure(background='#9b9b9b')
# Large Frame
self.win_frame = tk.Frame(self.root, width=670, height=520, highlightbackground='#595959', highlightthickness=2)
# menu (left side)
self.menu = tk.Frame(self.win_frame, width=150, height=516, highlightbackground='#595959', highlightthickness=2)
self.menu_label = tk.Label(self.menu, text="Settings", bg='#8a8a8a', font=("Courier", "16", "bold roman"))
self.mute = tk.Button(self.menu, text="XXXX", font="Courier", bg='#bcbcbc', activebackground='#cdcdcd')
# pygame
self.pygame_frame = tk.Frame(self.win_frame, width=514, height=514, highlightbackground='#595959', highlightthickness=2)
self.embed = tk.Frame(self.pygame_frame, width=512, height=512,)
# Packing
self.win_frame.pack(expand=True)
self.win_frame.pack_propagate(0)
self.menu.pack(side="left")
self.menu.pack_propagate(0)
self.menu_label.pack(ipadx=60, ipady=2)
self.mute.pack(ipadx=40, ipady=2, pady=5)
self.pygame_frame.pack(side="left")
self.embed.pack()
#This embeds the pygame window
os.environ['SDL_WINDOWID'] = str(self.embed.winfo_id())
if platform.system == "Windows":
os.environ['SDL_VIDEODRIVER'] = 'windib'
#Start pygame
pygame.init()
self.win = pygame.display.set_mode((512, 512))
self.win.fill(pygame.Color(255, 255, 255))
pygame.display.init()
self.root.mainloop()
screen = window()
#Here is sample code that I want to run
pygame.draw.rect(screen.win, (0, 0, 255), (200, 200, 100, 100))
当我使用pygame.draw.rect(screen.win, (0, 0, 255), (200, 200, 100, 100)) 时,没有任何反应。在类中使用 pygame 是可行的,但在我更复杂的游戏中,对我的所有变量使用 self.variable 似乎是不必要的。
如何在窗口类之外的 pygame 窗口中运行我的代码?
【问题讨论】:
-
我正在检查这个。与此同时 - 我刚刚更新并推送到 Pypi 一组用于 pygame 的小部件 - 尝试
pip install pygame-pgu- 示例和一些文档位于:github.com/parogers/pgu - 也许你不需要 tkinter。
标签: python tkinter pygame embed