Dec-06-2021, 09:56 PM
Good afternoon/morning,
My first post on this forum so hello to all other forum members. I have posted below a virtual keyboard for use on a touchscreen that i have... uh... written.
I am still very much a beginner in the realm of the python programming despite numerous complicated projects using the language i always feel its very verbose and computationally heavy.
Could those in the knowledge review my code below and perhaps suggest modifications particularly on why it is so temperamental to detect button presses/clicks. I realize some aspects may be missing such as highlight colours.
Main.py
My first post on this forum so hello to all other forum members. I have posted below a virtual keyboard for use on a touchscreen that i have... uh... written.
I am still very much a beginner in the realm of the python programming despite numerous complicated projects using the language i always feel its very verbose and computationally heavy.
Could those in the knowledge review my code below and perhaps suggest modifications particularly on why it is so temperamental to detect button presses/clicks. I realize some aspects may be missing such as highlight colours.
Main.py
import pygame from VirtualKeyboardLibrary import VirtualKeyboard pygame.init() # Setups SCREEN_HEIGHT = 600 SCREEN_WIDTH = 1024 SCREEN_MODE = [SCREEN_WIDTH, SCREEN_HEIGHT] SCREEN = pygame.display.set_mode(SCREEN_MODE) my_keyboard = VirtualKeyboard(SCREEN) def game_loop(): running = True while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False my_keyboard.draw() pygame.display.update() pygame.quit() quit() game_loop()VirtualKeyboardLibrary.py
import pygame class VirtualKeyboard: def __init__(self, surface, button_height=50, button_width=54, button_space_width=100, colour=(0, 0, 255), highlight_colour=(0, 0, 100), click_colour=(0, 0, 50), font="Arial", font_size=20, text_colour=(255, 255, 255), text_highlight_colour=(200, 200, 200), text_click_colour=(0, 0, 0), margin_top=4, margin_left=4, text_margin_left=8, text_margin_top=0): self.surface = surface self.button_height = button_height self.button_width = button_width self.button_space_width = button_space_width self.colour = colour self.highlight_colour = highlight_colour self.click_colour = click_colour self.font = font self.font_size = font_size self.text_colour = text_colour self.text_highlight_colour = text_highlight_colour self.text_click_colour = text_click_colour self.margin_top = margin_top self.margin_left = margin_left self.text_margin_left = text_margin_left self.text_margin_top = text_margin_top self.var_row = 2 self.var_column = 0 self.var_x = margin_left self.var_y = margin_top self.buttons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '<-', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'ENTER', ' SPACE '] self.main_font = pygame.font.SysFont(self.font, self.font_size) self.result = "" def draw(self): for button in self.buttons: if button != ' SPACE ': button_width_calc = self.button_width new_y = self.var_y new_x = self.var_x if button == 'ENTER': button_width_calc = self.button_width + 30 new_y = self.var_y + 60 new_x = self.var_x - 68 my_square = pygame.Rect(new_x, new_y, button_width_calc, self.button_height) self.update(my_square, button) pygame.draw.rect(self.surface, self.colour, [new_x, new_y, button_width_calc, self.button_height]) text = self.main_font.render(button, True, self.text_colour) self.surface.blit(text, (new_x + self.text_margin_left, new_y + self.text_margin_top)) elif button == ' SPACE ': x_offset = 408 y_offset = 60 width = 204 my_square = pygame.Rect(self.var_x - x_offset, self.var_y + y_offset, self.button_width + width, self.button_height) self.update(my_square, button) pygame.draw.rect(self.surface, self.colour, [self.var_x - x_offset, self.var_y + y_offset, self.button_width + width, self.button_height]) text = self.main_font.render(button, True, self.text_colour) self.surface.blit(text, ( self.var_x + self.text_margin_left - x_offset, self.var_y + self.text_margin_top + y_offset)) self.update_position() self.var_row = 2 self.var_column = 0 self.var_x = self.margin_top self.var_y = self.margin_left def update_position(self): self.var_column += 1 self.var_x += 68 if self.var_column > 10 and self.var_row == 2: self.var_column = 0 self.var_x = 4 self.var_y += 60 self.var_row += 1 elif self.var_column > 9 and self.var_row == 3: x_offset = 30 self.var_column = 0 self.var_x = 4 + x_offset self.var_y += 60 self.var_row += 1 elif self.var_column > 8 and self.var_row == 4: x_offset = 98 self.var_column = 0 self.var_x = 4 + x_offset self.var_y += 60 self.var_row += 1 def update(self, rect, char): mouse = pygame.mouse.get_pos() if rect.collidepoint(mouse): self.colour = (200, 200, 200) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if char == "<-": self.result = self.result[:-1] elif char == " SPACE ": self.result = self.result + ' ' else: self.result = self.result + char print(self.result) else: self.colour = (0, 0, 255)