【pygame-GUI (1.0)】教程——小试牛刀参考答案:简易甚至是简陋的计算器
本帖最后由 JasonChencCjj 于 2023-8-17 14:06 编辑万众瞩目的参考答案它来啦!
主贴
注意:
1、仅供参考!
2、gui界面的开发主流是使用pyqt、tkinter、ttk。。。
故不建议使用pygame(pygame主要用于开发游戏)
此库及教程主要让大家更方便的使用pygame写游戏UI
3、参考答案的计算器比较简陋,仅作为参考,优化空间很大,
(只做了加减法,输入过长的数字会导致显示出错)
所以各位可以把作品发布在评论区或者另开帖子分享
展示:
代码:
import pygame
import sys
from pygameGUI import*
from pygame.locals import *
# 颜色常量
WHITE = (255,255,255)
BLACK = (0,0,0)
size = width, height = 800,600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("pygame-GUI-Button")
#GUI组
guis = Group()
# 生成控件
#窗口
root = Frame(size=,title=True)
title = Label(text="计算器",font=("和音体.ttf",20))
root.set_title(text=title,pad=)
root.set_pos(position=)
guis.add(root)
#显示
var = Variable("")
def draw(image):
image.fill((230,230,230))
image.blit(Effect.sunken(image,width=5),)
monitor = Frame(master=root,size=,draw=draw)
monitor.set_pos("top" , 50)
monitor.set_pos("left" , 25)
guis.add(monitor)
l1 = Label(master=monitor,text="0",font=(None,100),color=(0,0,0),pos=(10,10))
guis.add(l1)
# 按钮
def callback():
var.set(var.get()+"1")
l1.set_text(var.get())
text = Label(text=" 1 ",font=(None,20))
b1 = Button(master=root,text=text,pad=,command=callback)
b1.set_pos("top",monitor.rect.bottom+20)
b1.set_pos("left",25)
guis.add(b1)
def callback():
var.set(var.get()+"2")
l1.set_text(var.get())
text = Label(text=" 2 ",font=(None,20))
b2 = Button(master=root,text=text,pad=,command=callback)
b2.set_pos("top",monitor.rect.bottom+20)
b2.set_pos("left",b1.rect_in_master.right+20)
guis.add(b2)
def callback():
var.set(var.get()+"3")
l1.set_text(var.get())
text = Label(text=" 3 ",font=(None,20))
b3 = Button(master=root,text=text,pad=,command=callback)
b3.set_pos("top",monitor.rect.bottom+20)
b3.set_pos("left",b2.rect_in_master.right+20)
guis.add(b3)
def callback():
var.set(var.get()+"+")
l1.set_text(var.get())
text = Label(text=" + ",font=(None,20))
b_add = Button(master=root,text=text,pad=,command=callback)
b_add.set_pos("top",monitor.rect.bottom+20)
b_add.set_pos("left",b3.rect_in_master.right+20)
guis.add(b_add)
def callback():
var.set(var.get()+"4")
l1.set_text(var.get())
text = Label(text=" 4 ",font=(None,20))
b4 = Button(master=root,text=text,pad=,command=callback)
b4.set_pos("top",b1.rect_in_master.bottom+20)
b4.set_pos("left",25)
guis.add(b4)
def callback():
var.set(var.get()+"5")
l1.set_text(var.get())
text = Label(text=" 5 ",font=(None,20))
b5 = Button(master=root,text=text,pad=,command=callback)
b5.set_pos("top",b1.rect_in_master.bottom+20)
b5.set_pos("left",b1.rect_in_master.right+20)
guis.add(b5)
def callback():
var.set(var.get()+"6")
l1.set_text(var.get())
text = Label(text=" 6 ",font=(None,20))
b6 = Button(master=root,text=text,pad=,command=callback)
b6.set_pos("top",b1.rect_in_master.bottom+20)
b6.set_pos("left",b5.rect_in_master.right+20)
guis.add(b6)
def callback():
var.set(var.get()+"-")
l1.set_text(var.get())
text = Label(text=" - ",font=(None,20))
b_sub = Button(master=root,text=text,pad=,command=callback)
b_sub.set_pos("top",b1.rect_in_master.bottom+20)
b_sub.set_pos("left",b6.rect_in_master.right+20)
guis.add(b_sub)
def callback():
var.set(var.get()+"7")
l1.set_text(var.get())
text = Label(text=" 7 ",font=(None,20))
b7 = Button(master=root,text=text,pad=,command=callback)
b7.set_pos("top",b4.rect_in_master.bottom+20)
b7.set_pos("left",25)
guis.add(b7)
def callback():
var.set(var.get()+"8")
l1.set_text(var.get())
text = Label(text=" 8 ",font=(None,20))
b8 = Button(master=root,text=text,pad=,command=callback)
b8.set_pos("top",b4.rect_in_master.bottom+20)
b8.set_pos("left",b1.rect_in_master.right+20)
guis.add(b8)
def callback():
var.set(var.get()+"9")
l1.set_text(var.get())
text = Label(text=" 9 ",font=(None,20))
b9 = Button(master=root,text=text,pad=,command=callback)
b9.set_pos("top",b4.rect_in_master.bottom+20)
b9.set_pos("left",b2.rect_in_master.right+20)
guis.add(b9)
def callback():
l1.set_text(str(eval(var.get())))
var.set("")
text = Label(text=" = ",font=(None,20))
b_equ = Button(master=root,text=text,pad=,command=callback)
b_equ.set_pos("top",b4.rect_in_master.bottom+20)
b_equ.set_pos("left",b3.rect_in_master.right+20)
guis.add(b_equ)
def callback():
var.set(var.get()+"0")
l1.set_text(var.get())
text = Label(text=" 0 ",font=(None,20))
b0 = Button(master=root,text=text,pad=,command=callback)
b0.set_pos("top",b7.rect_in_master.bottom+20)
b0.set_pos("left",25)
guis.add(b0)
def callback():
var.set(var.get()+".")
l1.set_text(var.get())
text = Label(text=". ",font=(None,20))
b_poi = Button(master=root,text=text,pad=,command=callback)
b_poi.set_pos("top",b7.rect_in_master.bottom+20)
b_poi.set_pos("left",b0.rect_in_master.right+20)
guis.add(b_poi)
def callback():
var.set(var.get())
l1.set_text(var.get())
text = Label(text="<- ",font=(None,20))
b_del = Button(master=root,text=text,pad=,command=callback)
b_del.set_pos("top",b7.rect_in_master.bottom+20)
b_del.set_pos("left",b_poi.rect_in_master.right+20)
guis.add(b_del)
clock = pygame.time.Clock()
delay = 60 # 延时计时器(1秒)
# 是否全屏
fullscreen = False
screen_change = False
# 背景颜色设定
bg_color = BLACK
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:
guis.press()
if event.type == MOUSEBUTTONUP:
if event.button == 1:
guis.release()
# 按键按下事件
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.move()
#画背景
screen.fill(bg_color)
# 刷新guis
guis.update(pos,rel)
# 画guis
guis.draw(screen)
# 刷新界面
pygame.display.update()
建议优化方向
基础:
1、添加乘除法
2、添加光标
3、屏幕仅显示当前操作步骤,其他存放于内存中
。。。。。
进阶:
1、模式切换:二进制模式、十六进制模式、日期计算、单位换算。。。。
2、平方,开方,百分号。。。。。
3、方程
。。。。。
传说:
1、顶
2、评论
3、加入专辑
4、评分
5、{:10_307:}
{:10_256:}{:10_256:} 不二如是 发表于 2023-8-17 18:04
不二一笑,精华难料{:10_256:} 歌者文明清理员 发表于 2023-8-17 22:48
不二一笑,精华难料
孩怕 上去,让更多人看到
页:
[1]