#coding=utf-8import os,sys,re,time import pygame import random from win32api import GetSystemMetrics from tkinter import messageboxpygame.init() pygame.display.set_caption("我的控件")percent = 0.6 screen_width = GetSystemMetrics(0) screen_height = GetSystemMetrics(1) window_width = int(screen_width*percent) window_height = int(screen_height*percent)dt = 0 clock = pygame.time.Clock()screen = pygame.display.set_mode((window_width, window_height))#停止处理输入法事件 pygame.key.stop_text_input()def showMsg(msg):messagebox.showinfo('提示信息', msg)class Button:def __init__(self, x, y, w, h):self.x = xself.y = yself.w = wself.h = hself.color = 'gray'self.text = '按钮'self.text_color = 'black'self.text_size = 12self.border_width = 1self.border_color = 'black'self.font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc')self.my_font = pygame.font.Font(self.font_path, self.text_size)def setColor(self, color):self.color = colordef setText(self, text):self.text = textdef getText(self):return self.textdef setTextColor(self, text_color):self.text_color = text_colordef setTextSize(self, text_size):self.text_size = text_sizeself.my_font = pygame.font.Font(self.font_path, self.text_size)def setBorderWidth(self, border_width):self.border_width = border_widthdef setBorderColor(self, border_color):self.border_color = border_colordef draw(self, win):pygame.draw.rect(win, self.color, (self.x, self.y, self.w, self.h))if self.border_width > 0:pygame.draw.rect(win, self.border_color, (self.x, self.y, self.w, self.h), self.border_width)text = self.my_font.render(self.text, True, self.text_color)myx = self.x + (self.w - text.get_width()) / 2myy = self.y + (self.h - text.get_height()) / 2win.blit(text, (myx, myy))def click(self, event):if self.x + self.w > event.pos[0] > self.x and self.y + self.h > event.pos[1] > self.y:return Truereturn Falseclass Label:def __init__(self, x, y, w, h):self.x = xself.y = yself.w = wself.h = hself.color = 'white'self.text = ''self.text_color = 'black'self.text_size = 12self.border_width = 0self.border_color = ''self.font_path = os.path.join(os.path.dirname(sys.argv[0]), 'simsun.ttc')self.my_font = pygame.font.Font(self.font_path, self.text_size)def setColor(self, color):self.color = colordef setText(self, text):self.text = textdef getText(self):return self.textdef setTextColor(self, text_color):self.text_color = text_colordef setTextSize(self, text_size):self.text_size = text_sizeself.my_font = pygame.font.Font(self.font_path, self.text_size)def setBorderWidth(self, border_width):self.border_width = border_widthdef setBorderColor(self, border_color):self.border_color = border_colordef getCharWH(self):padding_percent_width = 0.3padding_percent_height = 0.3test_text1 = '测试字符串'test_text2 = self.my_font.render(test_text1, True, self.text_color)char_width = test_text2.get_width() / len(test_text1)char_height = test_text2.get_height()padding_width = char_width * padding_percent_widthpadding_height = char_height * padding_percent_heightline_max_char = int((self.w - padding_width * 2) / char_width)return (char_height, padding_width, padding_height, line_max_char)def getTrueLines(self, char_height, padding_width, padding_height, line_max_char):texts = self.text.split("\n")k = 0for i,mytext in enumerate(texts):while len(mytext) > line_max_char:submytext = mytext[0:line_max_char]mytext = mytext[line_max_char:]k += 1k += 1return k+1def draw(self, win):(char_height, padding_width, padding_height, line_max_char) = self.getCharWH()lineNum = self.getTrueLines(char_height, padding_width, padding_height, line_max_char)if lineNum * char_height > self.h:self.h = lineNum * char_heightpygame.draw.rect(win, self.color, (self.x, self.y, self.w, self.h))if self.border_width > 0:pygame.draw.rect(win, self.border_color, (self.x, self.y, self.w, self.h), self.border_width)texts = self.text.split("\n")k = 0for i,mytext in enumerate(texts):while len(mytext) > line_max_char:submytext = mytext[0:line_max_char]subtext = self.my_font.render(submytext, True, self.text_color)submyx = self.x + padding_widthsubmyy = self.y + padding_height + char_height * kwin.blit(subtext, (submyx, submyy))mytext = mytext[line_max_char:]k += 1text = self.my_font.render(mytext, True, self.text_color)myx = self.x + padding_widthmyy = self.y + padding_height + char_height * kwin.blit(text, (myx, myy))k += 1bt = Button(5, 5, 80, 25) bt.setText('测试按钮') bt.setColor('Brown') bt.setTextColor('Gold') bt.setBorderColor('Lime') bt.setBorderWidth(1)label_text = ''' 我我我我我我我我我我我我我我我我我我我我我我我我 111111111111111111111111111111111111111111111 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx .。。。。。。。。。。。。。。。。。。。。。。。。 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '''.strip() label = Label(115, 5, 250, 250) label.setColor('Maroon') label.setText(label_text) label.setTextSize(18) label.setBorderColor('Lime') label.setBorderWidth(1)running = True while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.MOUSEBUTTONDOWN:if bt.click(event):showMsg("%s被点击了" % bt.getText())keys_pressed = pygame.key.get_pressed()#ESC键if keys_pressed[pygame.K_ESCAPE]:running = Falsescreen.fill("purple")bt.draw(screen)label.draw(screen)#更新显示pygame.display.flip()#pygame.display.update()dt = clock.tick(60) / 600pygame.quit()
效果: