pygameGUU2.0 投票 书否需要多选与单选按钮?
本帖最后由 cjjJasonchen 于 2023-9-8 13:11 编辑pygameGUU2.0 投票书否需要多选与单选按钮?
主贴:
pygameGUI 2.0 开发日志
https://fishc.com.cn/thread-232764-1-1.html
(出处: 鱼C论坛)
可以看看我主贴写的“小测试”
这几天我一直在尝试写一些多选按钮的预设,我想这样或许可以更方便的制作多选按钮,像是tkinter那样
我花费了大量的时间和精力,希望可以让“制作选择按钮”方便一点
然而呢,今天在写的时候,我突然意识到,或许这真的没有必要做。
理由如下:
1、在游戏开发中,我们几乎不会像使用tkinter那样有这么多机会使用多选或单选按钮
2、现有的按钮组件也可以轻松完成这样的任务(见主贴:《小测试:多选按钮与单选按钮》)
3、这一点是我不想写的主要原因,我不知道写一个预设是不是真的能变得更方便,原本要自定义的还是要自定义,
只是使用者们在 command函数 里面可以少些几行代码罢了
而且现在的button支持传参了,完成多选和单选并不复杂
下面来提一提原本我希望的选项按钮:
每次点击要自己切换外观,如:从未选择变为选择,
改变command函数,如:从在variable中添加变为删除。。。。。。
现在我决定在Effect 里面多做一些image效果,方便使用者完成“切换外观“的操作
至于其他的,用户自己写在command里面就好了
我都在说什么乱七八糟的
总之呢,就是想要通知各位:checkButton和rediobutton可能不会有了,毕竟现在button可以很好的完成这个任务。。。。
嘛,反正大家畅所欲言,把想法发在评论区,讨论一下,
说不定等我过几天技术变得更好的就可以做了{:10_297:} (我不会告诉你们是因为我做了好几天没做出来的)
多选与单选的体验代码我也发出来给大家看看把~放在第二页~
模块:
import pygame
import sys
from pygame.locals import *
pygame.init()
class Effect():
"""pygame-GUI专用的默认特效,
输入image,输出image
把输出的image对象,会返回带有效果的image对象"""
def light(image=None,color=(255,255,255),width=0,alpha=(50)):
image = image.copy() # image原图
rect = image.get_rect()
n_image = pygame.Surface(,rect], pygame.SRCALPHA) # n_image要贴在原图上的新图
pygame.draw.rect(n_image,color,rect,width)
n_image.set_alpha(alpha)
return n_image
def dark(image=None,color=(0,0,0),width=0,alpha=(50)):
image = image.copy() # image原图
rect = image.get_rect()
n_image = pygame.Surface(,rect], pygame.SRCALPHA) # n_image要贴在原图上的新图
pygame.draw.rect(n_image,color,rect,width)
n_image.set_alpha(alpha)
return n_image
def raised(image=None,light_color=(255,255,255),dark_color = (0,0,0),alpha=255,width=3):
image = image.copy() # image原图
rect = image.get_rect()
o_image = pygame.Surface(,rect], pygame.SRCALPHA)
depth=0
for i in range(width):
n_image = pygame.Surface(,rect], pygame.SRCALPHA) # n_image要贴在原图上的新图
depth = int((light_color/width)*(width-i))
pygame.draw.line(n_image,light_color,,-i,i])
pygame.draw.line(n_image,light_color,,-i])
pygame.draw.line(n_image,dark_color,-i,i],-i,rect-i])
pygame.draw.line(n_image,dark_color,-i],-i,rect-i])
n_image.set_alpha(depth)
o_image.blit(n_image,)
o_image.set_alpha(alpha)
return o_image
def sunken(image=None,light_color=(255,255,255),dark_color = (0,0,0),alpha=255,width=3):
image = image.copy() # image原图
rect = image.get_rect()
o_image = pygame.Surface(,rect], pygame.SRCALPHA)
depth=0
for i in range(width):
n_image = pygame.Surface(,rect], pygame.SRCALPHA) # n_image要贴在原图上的新图
depth = int((light_color/width)*i)
pygame.draw.line(n_image,light_color,-i],-i,rect-i])
pygame.draw.line(n_image,light_color,-i,i],-i,rect-i])
pygame.draw.line(n_image,dark_color,,-i,i])
pygame.draw.line(n_image,dark_color,,-i])
n_image.set_alpha(depth)
o_image.blit(n_image,)
o_image.set_alpha(alpha)
return o_image
class Variable():
"""pygame-GUI专用的变量,支持python的数据类型"""
def __init__(self,value=None):
"""value : 设定初始值"""
self.value = value
def get(self):
"""得到变量的值"""
return self.value
def set(self,value):
"""设置变量的值"""
self.value = value
class Group(pygame.sprite.Group):
"用来控制UI的组"
def __init__(self):
super().__init__()
self.pos = 0
self.rel = 0
# 用于判断执行函数的列表,会同步精灵组删除
self.widgets = {
Button : [],
Label : [],
Frame : [],
Slider : [],
}
self.select_list = []
def update(self,pos=None,rel=None):
self.pos = pos
self.rel = rel
for widget in self.sprites():
# 检测是否需要删除
if widget not in self.sprites():
widgets.remove(widget)
# 初始化鼠标位置下部件的列表
self.select_list = []
# 检测所有按钮
for button in self.widgets:
# 检测所有部件是否在鼠标下方
if button.rect.left < self.pos < button.rect.right and \
button.rect.top < self.pos < button.rect.bottom \
and button.status != "down":
button.status = "none"
self.select_list.append(button)
elif button.status != "down":
button.status = "none"
# 检测所有滑块
for slider in self.widgets:
# 检测所有部件是否在鼠标下方
if slider.rect.left < self.pos < slider.rect.right and \
slider.rect.top < self.pos < slider.rect.bottom \
and slider.status != "down":
slider.status = "none"
self.select_list.append(slider)
elif slider.status != "down":
slider.status = "none"
slider.move(self.rel)
# 得出被选中的组件
if self.select_list:
if self.select_list[-1].status == "none":
self.select_list[-1].status = "select"
for widget in self.select_list:
widget.status = "none"
super().update()
def decide(self, event):
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
"鼠标按下时调用"
if self.select_list:
self.select_list[-1].press()
self.select_list[-1].status = "down"
if event.type == MOUSEBUTTONUP:
if event.button == 1:
"鼠标松开时调用"
for widget in self.sprites():
widget.release()
widget.status = "none"
def add(self,widget=None):
if widget:
super().add(widget)
self.widgets.append(widget)
class UI(pygame.sprite.Sprite):
"""所有ui的基类"""
# "select"/"down"/"none"
#当鼠标处于上方时:"select",鼠标按下:"down",鼠标抬起,且不位于此部件上方:"none"
def __init__(self):
super().__init__()
def press(self):
"鼠标按下此部件时调用"
pass
def release(self):
"鼠标抬起时调用(所有部件都会调用)"
pass
def set_pos(self,location="center",position=):
"提供了更多的位置设置方法"
class LocationNounError(Exception):
def __init__(self,location):
self.location = location
def __str__(self):
return "位置名词错误:{self.location}"
if location == "center":
self.rect_in_master.center = position
elif location == "top":
self.rect_in_master.top = position
elif location == "bottom":
self.rect_in_master.bottom = position
elif location == "right":
self.rect_in_master.right = position
elif location == "left":
self.rect_in_master.left = position
else:
raise LocationNounError(location)
class Button(UI):
"""生成一个UI对象 -> 当鼠标单击这个对象时调用command中输入的函数"""
# "select"/"down"/"none"
#当鼠标处于上方时:"select",鼠标按下:"down",鼠标抬起,且不位于此部件上方:"none"
status = "none"
def __init__(self, master=None,pos=, draw=, text=None, pad=, size=, command=None, variable=None,alpha=255,repeat=-1):
"""
master : 该对象绘制的对象(跟随移动,而不是真的绘制在上面),如果None就是绘制在screen上 -> Frame
pos : 该对象左上角的坐标位 ->
draw :
1、按钮填充该颜色 -> / (R,G,B)
2、按钮的绘制函数 draw(image) -> def draw(image):
image.fill((255,0,0))
pygame.draw.rect(image,......
pygame.draw.circle(image,......
size : 大小,如果设定了text值,则该属性无效 ->
text : 绘制在draw函数绘制的图像上的文本 -> Text对象
pad : 当设定text值时有效,将按钮的大小(size)设定为Text对象的(width+pad,height+pad) ->
command : 当该对象被点击时调用的函数 -> callback(函数名)
variable : 当该对象被点击时,command中函数的返回值 Variable
repeat : 当该对象被点击时,以repeat的频率,触发command函数int(单位:帧)
"""
super().__init__()
self.text = text
self.variable = variable
self.command = command
self.master = master
self.repeat = repeat
self.total_repeat = repeat
# 生成Surface对象
if text:
self.none_image = pygame.Surface(*2,text.height+pad*2], pygame.SRCALPHA)
else:
self.none_image = pygame.Surface(size, pygame.SRCALPHA)
self.image = self.none_image
self.rect = self.image.get_rect() # 在screen的位置
self.rect.x, self.rect.y = pos
# 如果有master,rect_in_master为在master的位置,否则rect_in_master等同rect
if self.master:
self.rect_in_master = self.rect.copy()
self.rect.x += self.master.rect.x
self.rect.y += self.master.rect.y
master.guis.add(self) #在master的组中加入自己
else:
self.rect_in_master = self.rect
# 绘制
if type(draw) == tuple or type(draw) == list:
self.none_image.fill(draw)
else:
draw(self.none_image)
if text:
# 生成文本
self.none_image.blit(self.text.image,,pad])
# 设置 按下和准备选择状态下的默认图片
self.selected_image = self.none_image.copy()
self.selected_image.blit(Effect.light(self.none_image),)
self.down_image = self.none_image.copy()
self.down_image.blit(Effect.dark(self.none_image,alpha=(20)),)
def set_command(self,command):
"""设置command"""
self.command = command
def press(self):
super().press()
center = self.rect.center
self.image = self.down_image
self.rect = self.image.get_rect()
self.rect.center = center
def release(self):
if self.status == "down":
if self.variable:
self.variable.set(self.command())
else:
self.command()
self.repeat = self.total_repeat
super().release()
def update(self):
if self.status == "select" and self.image!= self.selected_image:
center = self.rect.center
self.image = self.selected_image
self.rect = self.image.get_rect()
self.rect.center = center
elif self.status == "none" and self.image!= self.none_image:
center = self.rect.center
self.image = self.none_image
self.rect = self.image.get_rect()
self.rect.center = center
elif self.status == "down":
self.repeat -= 1
if self.repeat == 0:
self.repeat = self.total_repeat
self.variable = self.command()
elif self.repeat < -999:
self.repeat = -1
# 跟随master移动
if self.master:
self.rect.x = self.rect_in_master.x + self.master.rect.x
self.rect.y = self.rect_in_master.y + self.master.rect.y
def set_none_image(self,draw,size=,text=None,pad=):
# 生成Surface对象
if text:
self.none_image = pygame.Surface(*2,text.height+pad*2], pygame.SRCALPHA)
else:
self.none_image = pygame.Surface(size, pygame.SRCALPHA)
# 绘制
if type(draw) == type(tuple) or type(draw) == (list):
self.none_image.fill(draw)
else:
draw(self.none_image)
if text:
# 生成文本
self.none_image.blit(text.image,,pad])
def set_selected_image(self,draw,size=,text=None,pad=):
# 生成Surface对象
if text:
self.selected_image = pygame.Surface(*2,text.height+pad*2], pygame.SRCALPHA)
else:
self.selected_image = pygame.Surface(size, pygame.SRCALPHA)
# 绘制
if type(draw) == type(tuple) or type(draw) == (list):
self.selected_image.fill(draw)
else:
draw(self.selected_image)
if text:
# 生成文本
self.selected_image.blit(text.image,,pad])
def set_down_image(self,draw,size=,text=None,pad=):
# 生成Surface对象
if text:
self.down_image = pygame.Surface(*2,text.height+pad*2], pygame.SRCALPHA)
else:
self.down_image = pygame.Surface(size, pygame.SRCALPHA)
# 绘制
if type(draw) == tuple or type(draw) == list:
self.down_image.fill(draw)
else:
draw(self.down_image)
if text:
# 生成文本
self.down_image.blit(text.image,,pad])
class Label(UI):
"""生成一个sprite对象 -> 可以使用精灵组更好的管理单行文本对象"""
def __init__(self,
master=None,
pos=,
text=" ",
font=(None,15),
antialias=True,
color=,
background=None,
alpha=255,
bold = False,
italic = False,
underline = False,
width = 0,
align="left"):
"""
pos : 左上角位置
text : 文本 str
font : 字体和字号(字体(.ttf文件/pygame系统字体名称),字号(int))
antialias : 抗锯齿 bool
color : 字体颜色(RGB)
background : 背景颜色(RGB) None表示透明
alpha : 透明度 (int : 0-255)
bold : 是否加粗 -> bool
italic : 是否斜体 -> bool
underline : 是否有下划线 -> bool
width : 如果不为0,超过此长度将自动换行(同时,决定了是否支持换行符) -> int(单位:像素) 注意:此值不能过小,否则会卡死
align : 对其方式->"center"/"left"/"right"
"""
super().__init__()
self.pos = pos
self.text = text
self.master = master
self.antialias = antialias
self.color = color
self.background = background
self.alpha = alpha
self.italic = italic
self.bold = bold
self.underline = underline
self.width = width
self.align = align
# 生成字体对象
try:
self.font = pygame.font.Font(font,font)
except:
self.font = pygame.font.SysFont(font,font,self.bold,self.italic)
# 下划线 如果字体没有加粗/斜体,强行加粗/倾斜
self.font.set_underline(self.underline)
if not self.font.get_bold():
self.font.set_bold(self.bold)
if not self.font.get_italic():
self.font.set_italic(self.italic)
# 换行
if self.width:
image_list = []
line = ""
for word in self.text:
if self.font.size(line) <= self.width:
if word == "\n":
image_list.append(self.font.render(line,antialias,color,background))
line = ""
else:
line += word
else:
last_word = line[-1]
line = line
image_list.append(self.font.render(line,antialias,color,background))
line = ""
line += last_word
line += word
if line: # 解决line中可能还有剩余
image_list.append(self.font.render(line,antialias,color,background))
# 生成image
self.line_height = self.font.get_height()
self.height = self.line_height * len(image_list)
self.image = pygame.Surface(, pygame.SRCALPHA)
# 对齐
for i in image_list:
if self.align == "left":
self.image.blit(i,)
elif self.align == "right":
self.image.blit(i,,image_list.index(i)*self.font.get_height()])
elif self.align == "center":
self.image.blit(i,[(self.width-i.get_rect())/2,image_list.index(i)*self.font.get_height()])
else:
self.image = self.font.render(self.text,antialias,color,background)
self.image.set_alpha(self.alpha)
self.size = self.width, self.height = self.font.size(self.text) # (width,height)
# 设置位置
self.rect = self.image.get_rect()
self.rect.x,self.rect.y = pos
# 如果有master,rect_in_master为在master的位置,否则rect_in_master等同rect
if self.master:
self.rect_in_master = self.rect.copy()
self.rect.x += self.master.rect.x
self.rect.y += self.master.rect.y
master.guis.add(self) #在master的组中加入自己
else:
self.rect_in_master = self.rect
def update(self):
# 跟随master移动
if self.master:
self.rect.x = self.rect_in_master.x + self.master.rect.x
self.rect.y = self.rect_in_master.y + self.master.rect.y
def set_text(self,text):
self.image = self.font.render(text,self.antialias,self.color,self.background)
self.image.set_alpha(self.alpha)
rect = self.rect
self.rect = self.image.get_rect()
self.rect = rect
class Frame(UI):
"""生成一个sprite对象 -> 类似与tkinter的frame"""
def __init__(self,master=None, pos=,size=,draw=(190,190,190),):
"""
pos : 左上角位置
size : 大小
draw :
1、在这个部件中填充该颜色 -> / (R,G,B)
2、输入一个的绘制函数 draw(image) -> def draw(image):
image.fill((255,0,0))
pygame.draw.rect(image,......
pygame.draw.circle(image,......
"""
super().__init__()
self.width ,self.height = size
self.image = pygame.Surface(size, pygame.SRCALPHA)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = pos
self.master = master
self.guis = Group()
# 绘制
if type(draw) == tuple or type(draw) == list:
self.image.fill(draw)
else:
draw(self.image)
# 如果有master,rect_in_master为在master的位置,否则rect_in_master等同rect
if self.master:
self.rect_in_master = self.rect.copy()
self.rect.x += self.master.rect.x
self.rect.y += self.master.rect.y
master.guis.add(self) #在master的组中加入自己
else:
self.rect_in_master = self.rect
def update(self):
# 跟随master移动
if self.master:
self.rect.x = self.rect_in_master.x + self.master.rect.x
self.rect.y = self.rect_in_master.y + self.master.rect.y
class Slider(UI):
# "select"/"down"/"none"
#当鼠标处于上方时:"select",鼠标按下:"down",鼠标抬起,且不位于此部件上方:"none"
status = "none"
def __init__(self,master=None,pos=, draw=, text=None, pad=, size=,command=None,variable=None,orient="xy",spill=False):
"""
master : 该对象绘制的对象(跟随移动,而不是真的绘制在上面),如果None就是绘制在screen上 -> Frame
pos : 该对象左上角的坐标位 ->
draw :
1、按钮填充该颜色 -> / (R,G,B)
2、按钮的绘制函数 draw(image) -> def draw(image):
image.fill((255,0,0))
pygame.draw.rect(image,......
pygame.draw.circle(image,......
size : 大小,如果设定了text值,则该属性无效 ->
text : 绘制在draw函数绘制的图像上的文本 -> Text对象
pad : 当设定text值时有效,将按钮的大小(size)设定为Text对象的(width+pad,height+pad) ->
command : 当该对象被点击时调用的函数 -> callback(函数名)
variable : 当该对象被点击时,command中函数的返回值 Variable
orient : 允许在哪个轴的方向上移动 -> str "x"/"y"/"xy"
spill : 决定了如何判定是否移出master的方法 -> bool (True:中心 False:整体)
"""
super().__init__()
self.text = text
self.variable = variable
self.command = command
self.master = master
self.orient = orient
self.rel =
self.spill = spill
# 生成Surface对象
if text:
self.none_image = pygame.Surface(*2,text.height+pad*2], pygame.SRCALPHA)
else:
self.none_image = pygame.Surface(size, pygame.SRCALPHA)
self.image = self.none_image
self.rect = self.image.get_rect() # 在screen的位置
self.rect.x, self.rect.y = pos
# 如果有master,rect_in_master为在master的位置,否则rect_in_master等同rect
if self.master:
self.rect_in_master = self.rect.copy()
self.rect.x += self.master.rect.x
self.rect.y += self.master.rect.y
master.guis.add(self) #在master的组中加入自己
else:
self.rect_in_master = self.rect
# 绘制
if type(draw) == tuple or type(draw) == list:
self.none_image.fill(draw)
else:
draw(self.none_image)
if text:
# 生成文本
self.none_image.blit(self.text.image,,pad])
# 设置 按下和准备选择状态下的默认图片
self.selected_image = self.none_image.copy()
self.selected_image.blit(Effect.light(self.none_image),)
self.down_image = self.none_image.copy()
self.down_image.blit(Effect.dark(self.none_image,alpha=(20)),)
def set_command(self,command):
"""设置command"""
self.command = command
def press(self):
super().press()
center = self.rect.center
self.image = self.down_image
self.rect,self.rect,self.rect,self.rect = self.image.get_rect()
self.rect.center = center
def move(self,rel):
"""移动滑块"""
class LocationNounError(Exception):
def __init__(self,location):
self.location = location
def __str__(self):
return "位置名词错误:{self.location}"
if self.status == "down":
if self.orient == "xy":
self.rect_in_master += rel
self.rect_in_master += rel
self.rel = rel
elif self.orient == "x":
self.rect_in_master += rel
self.rel = ,0]
elif self.orient == "y":
self.rect_in_master += rel
self.rel = ]
else:
raise LocationNounError(self.orient)
# 防止移出框架
if self.spill:
if self.rect_in_master.center < 0:
self.rect_in_master = self.rect_in_master.center
self.rel=
elif self.rect_in_master.center > self.master.rect:
self.rect_in_master = self.master.rect-self.rect_in_master.center
self.rel=
if self.rect_in_master.center < 0:
self.rect_in_master = -self.rect_in_master/2
self.rel=
elif self.rect_in_master.center > self.master.rect:
self.rect_in_master = self.master.rect-self.rect_in_master/2
self.rel=
else:
if self.rect_in_master < 0:
self.rect_in_master = 0
self.rel=
elif self.rect_in_master + self.rect_in_master > self.master.rect:
self.rect_in_master = self.master.rect-self.rect_in_master
self.rel=
if self.rect_in_master < 0:
self.rect_in_master = 0
self.rel=
elif self.rect_in_master + self.rect_in_master > self.master.rect:
self.rect_in_master = self.master.rect-self.rect_in_master
self.rel=
# 回调函数
if self.command:
if self.variable:
self.variable = self.command(self.rel)
else:
self.command(self.rel)
def update(self):
if self.status == "select" and self.image!= self.selected_image:
center = self.rect.center
self.image = self.selected_image
self.rect,self.rect,self.rect,self.rect = self.image.get_rect()
self.rect.center = center
elif self.status == "none" and self.image!= self.none_image:
center = self.rect.center
self.image = self.none_image
self.rect,self.rect,self.rect,self.rect = self.image.get_rect()
self.rect.center = center
# 跟随master移动
if self.master:
self.rect.x = self.rect_in_master.x + self.master.rect.x
self.rect.y = self.rect_in_master.y + self.master.rect.y
多选:
import pygame
import sys
import pygameGUI as pgui
from pygame.locals import *
pygame.init()
def main():
# 颜色常量
WHITE = (255,255,255)
BLACK = (0,0,0)
GRAY = (170,170,170)
size = width, height = 800,600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("title")
guis = pgui.Group() #UI组
var1 = pgui.Variable("")
var2 = pgui.Variable("")
l4 = pgui.Label(text="A",font=("youyuan",20),pos=(100,200),color=(255,255,255),underline=True)
guis.add(l4)
# 按钮1
def b1_callback1():
b1.set_command(b1_callback2)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
pygame.draw.rect(image,color=(0,0,0),rect=+3,rect+3,rect-6,rect-6])
b1.set_none_image(draw=draw,size=)
b1.set_selected_image(draw=draw,size=)
b1.set_down_image(draw=draw,size=)
#b2_callback2()
return "A"
def b1_callback2():
b1.set_command(b1_callback1)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b1.set_none_image(draw=draw,size=)
b1.set_selected_image(draw=draw,size=)
b1.set_down_image(draw=draw,size=)
return ""
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b1 = pgui.Button(draw = draw, size=,command=b1_callback1,variable=var1)
b1.set_pos("right",l4.rect.left-10)
b1.set_pos("bottom",l4.rect.bottom)
guis.add(b1)
l2 = pgui.Label(text="B",font=("youyuan",20),pos=(100,200),color=(255,255,255),underline=True)
guis.add(l2)
# 按钮2
def b2_callback1():
b2.set_command(b2_callback2)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
pygame.draw.rect(image,color=(0,0,0),rect=+3,rect+3,rect-6,rect-6])
b2.set_none_image(draw=draw,size=)
b2.set_selected_image(draw=draw,size=)
b2.set_down_image(draw=draw,size=)
#b1_callback2()
return "B"
def b2_callback2():
b2.set_command(b2_callback1)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b2.set_none_image(draw=draw,size=)
b2.set_selected_image(draw=draw,size=)
b2.set_down_image(draw=draw,size=)
return ""
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b2 = pgui.Button(draw = draw, size=,command=b2_callback1,variable=var2)
b2.set_pos("left",l4.rect.right+10)
b2.set_pos("bottom",l4.rect.bottom)
guis.add(b2)
l2.set_pos("left",b2.rect.right+10)
l3 = pgui.Label(text=var1.get(),font=("youyuan",20),pos=(100,200),color=(0,0,0),bold=True,background=(200,200,200))
guis.add(l3)
l3.set_pos("left",l2.rect.right+30)
clock = pygame.time.Clock()
delay = 60 # 延时计时器(1秒)
# 是否全屏
fullscreen = False
screen_change = False
# 背景颜色设定
bg_color = GRAY
running = True
while running:
pos = pygame.mouse.get_pos()
rel = pygame.mouse.get_rel()
# 设定帧数
clock.tick(60)
# 延时计时器刷新
if delay == 0:
delay = 60
delay -= 1
# 检测是否全屏
if fullscreen and screen_change:
screen = pygame.display.set_mode(size,FULLSCREEN,HWSURFACE)
screen_change = False
elif screen_change:
screen = pygame.display.set_mode(size)
screen_change = False
# 事件检测
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 鼠标
if event.type == MOUSEBUTTONDOWN:
if event.button == 1: # 左键按下,获取鼠标位置
pass
if event.type == MOUSEBUTTONUP:
if event.button == 1: # 左键松开,获取鼠标位置
pass
# 按键按下事件
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#F11切换全屏
if event.key == K_F11:
fullscreen = not fullscreen
screen_change = True
# 按键抬起事件
if event.type == KEYUP:
pass
guis.decide(event)
#画背景
screen.fill(bg_color)
# 刷新xxx
guis.update(pos=pos,rel=rel)import pygame
import sys
import pygameGUI as pgui
from pygame.locals import *
pygame.init()
def main():
# 颜色常量
WHITE = (255,255,255)
BLACK = (0,0,0)
GRAY = (170,170,170)
size = width, height = 800,600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("title")
guis = pgui.Group() #UI组
var = pgui.Variable(" ")
l4 = pgui.Label(text="A",font=("youyuan",20),pos=(100,200),color=(255,255,255),underline=True)
guis.add(l4)
# 按钮1
def b1_callback1():
b1.set_command(b1_callback2)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
pygame.draw.rect(image,color=(0,0,0),rect=+3,rect+3,rect-6,rect-6])
b1.set_none_image(draw=draw,size=)
b1.set_selected_image(draw=draw,size=)
b1.set_down_image(draw=draw,size=)
b2_callback2()
return "A"
def b1_callback2():
b1.set_command(b1_callback1)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b1.set_none_image(draw=draw,size=)
b1.set_selected_image(draw=draw,size=)
b1.set_down_image(draw=draw,size=)
return " "
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b1 = pgui.Button(draw = draw, size=,command=b1_callback1,variable=var)
b1.set_pos("right",l4.rect.left-10)
b1.set_pos("bottom",l4.rect.bottom)
guis.add(b1)
l2 = pgui.Label(text="B",font=("youyuan",20),pos=(100,200),color=(255,255,255),underline=True)
guis.add(l2)
# 按钮2
def b2_callback1():
b2.set_command(b2_callback2)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
pygame.draw.rect(image,color=(0,0,0),rect=+3,rect+3,rect-6,rect-6])
b2.set_none_image(draw=draw,size=)
b2.set_selected_image(draw=draw,size=)
b2.set_down_image(draw=draw,size=)
b1_callback2()
return "B"
def b2_callback2():
b2.set_command(b2_callback1)
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b2.set_none_image(draw=draw,size=)
b2.set_selected_image(draw=draw,size=)
b2.set_down_image(draw=draw,size=)
return " "
def draw(image):
rect = image.get_rect()
pygame.draw.rect(image,color=(0,0,0),rect=rect,width=1)
b2 = pgui.Button(draw = draw, size=,command=b2_callback1,variable=var)
b2.set_pos("left",l4.rect.right+10)
b2.set_pos("bottom",l4.rect.bottom)
guis.add(b2)
l2.set_pos("left",b2.rect.right+10)
l3 = pgui.Label(text=var.get(),font=("youyuan",20),pos=(100,200),color=(0,0,0),bold=True,background=(200,200,200))
guis.add(l3)
l3.set_pos("left",l2.rect.right+30)
clock = pygame.time.Clock()
delay = 60 # 延时计时器(1秒)
# 是否全屏
fullscreen = False
screen_change = False
# 背景颜色设定
bg_color = GRAY
running = True
while running:
pos = pygame.mouse.get_pos()
rel = pygame.mouse.get_rel()
# 设定帧数
clock.tick(60)
# 延时计时器刷新
if delay == 0:
delay = 60
delay -= 1
# 检测是否全屏
if fullscreen and screen_change:
screen = pygame.display.set_mode(size,FULLSCREEN,HWSURFACE)
screen_change = False
elif screen_change:
screen = pygame.display.set_mode(size)
screen_change = False
# 事件检测
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 鼠标
if event.type == MOUSEBUTTONDOWN:
if event.button == 1: # 左键按下,获取鼠标位置
pass
if event.type == MOUSEBUTTONUP:
if event.button == 1: # 左键松开,获取鼠标位置
pass
# 按键按下事件
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#F11切换全屏
if event.key == K_F11:
fullscreen = not fullscreen
screen_change = True
# 按键抬起事件
if event.type == KEYUP:
pass
guis.decide(event)
#画背景
screen.fill(bg_color)
# 刷新xxx
guis.update(pos=pos,rel=rel)
#画 xxxx
guis.draw(screen)
l3.set_text(var.get())
# 刷新界面
pygame.display.update()
if __name__ == "__main__":
main()
#画 xxxx
guis.draw(screen)
l3.set_text(var1.get()+var2.get())
# 刷新界面
pygame.display.update()
if __name__ == "__main__":
main()
单选:
妹人? 单季贴吧? 薛定谔的投票{:10_256:} 中{:10_254:} cjjJasonchen 发表于 2023-9-8 15:12
单季贴吧?
淡季{:10_256:} sfqxx 发表于 2023-9-8 17:32
中
中奖绝缘体 cjjJasonchen 发表于 2023-9-8 15:12
单季贴吧?
因为很多上学了,应该还有些寄宿学校的,兄弟们上论坛的时间减少了 {:10_257:} 中英文泡椒 发表于 2023-9-8 17:41
中奖绝缘体
{:10_243:} sfqxx 发表于 2023-9-8 17:31
薛定谔的投票
{:10_256:} 中英文泡椒 发表于 2023-9-8 17:43
因为很多上学了,应该还有些寄宿学校的,兄弟们上论坛的时间减少了
是这样的,同志们都很辛苦 Wei-Yuanzhe 发表于 2023-9-8 18:39
{:10_257:} 币 {:10_269:} cjjJasonchen 发表于 2023-9-8 18:46
你们多大啊{:9_241:} 币 {:10_256:} Wei-Yuanzhe 发表于 2023-9-8 19:20
你们多大啊
未成年 cjjJasonchen 发表于 2023-9-8 19:27
未成年
上初中了吗{:9_241:}
页:
[1]
2