PYGLET - 创建窗口
在本文中,我们将看到如何在Python中的 PYGLET 模块中创建窗口。 Pyglet 是一个易于使用但功能强大的库,用于在 Windows、Mac OS 和 Linux 上开发视觉丰富的 GUI 应用程序,如游戏、多媒体等。这个库完全是用Python创建的,它支持许多功能,如窗口、用户界面事件处理、操纵杆、OpenGL 图形、加载图像和视频以及播放声音和音乐。窗口是占用操作系统资源的“重量级”对象。 Windows 可能显示为浮动区域,也可以设置为填充整个屏幕(全屏)。浮动时,窗口可能显示为无边框或装饰有特定于平台的框架(例如,包括标题栏、最小化和关闭按钮、调整大小手柄等)。
In order to create window we use Window method with the pyglet.window
Syntax : pyglet.window.Window(width, height, title)
Argument : It takes optional arguments i.e width, height and title of window
Return : It returns pyglet.window.win32.Win32Window object
注意:要么我们不提供任何参数,要么我们必须证明所有三个参数,否则会产生错误
例子 :
Python3
# importing pyglet module
from pyglet import *
# width of window
width = 400
# height of window
height = 400
# caption i.e title of the window
title = "PYGLET GfG"
# creating a window
win = window.Window(width, height, title)
# start running the application
app.run()
Python3
# importing pyglet module
import pyglet
# width of window
width = 500
# height of window
height = 500
# caption i.e title of the window
title = "Geeksforgeeks"
# creating a window
window = pyglet.window.Window(width, height, title)
# creating alabel
label = pyglet.text.Label('GeeeksforGeeks',
font_name ='Times New Roman',
font_size = 36,
x = window.width//2, y = window.height//2,
anchor_x ='center', anchor_y ='center')
# drawing label
@window.event
def on_draw():
window.clear()
label.draw()
# start running the application
pyglet.app.run()
输出 :
另一个例子 :
蟒蛇3
# importing pyglet module
import pyglet
# width of window
width = 500
# height of window
height = 500
# caption i.e title of the window
title = "Geeksforgeeks"
# creating a window
window = pyglet.window.Window(width, height, title)
# creating alabel
label = pyglet.text.Label('GeeeksforGeeks',
font_name ='Times New Roman',
font_size = 36,
x = window.width//2, y = window.height//2,
anchor_x ='center', anchor_y ='center')
# drawing label
@window.event
def on_draw():
window.clear()
label.draw()
# start running the application
pyglet.app.run()