0

I am making a pygame program, using python 2.7.

I am currently integrating a gui so that one does not have to enter certain data, like the hostname of the server my program connects to, directly into the command line.

I have tried pgu, and even embedding the program in tkinter. Both approaches had major problems.

I am currently trying EzText, which looks much more promising. However, EzText, while collecting a text input, will always do so when updated. What is the best way to make it so that events are only collected when the text input was clicked on last?

Otherwise I want key events to be processed by a different event handling function.

I have tried creating a rect from the EzText x and y coordinates and colliding the mouse's location with it using pygame.rect.collidepoint, however, even if this did work, it is a poor solution to the problem. Here is my current code (or at least the pertinent parts):

class inputter():
    def __init__(self,prompt,x,y,output):
        self.nameBox = eztext.Input(maxlength=15, color=(255, 255, 255), prompt=prompt, font=pygame.font.SysFont("Times New Roman", 12))
        self.nameBox.set_pos(x,y)
        inputters.append(self)
        self.output = output
    def run(self,events):
        global screen
        global output1
        global output2
        global output3
        global output4
        for item in events:
            if item.type == pygame.KEYDOWN and item.key == pygame.K_RETURN:
                if self.output == 1:
                    output1 = self.getval
                if self.output == 2:
                    output2 = self.getval
                if self.output == 3:
                    output3 = self.getval
                if self.output == 4:
                    output4 = self.getval
        self.nameBox.update(events)
        self.nameBox.draw(screen)
        return None
    def getval(self):
        retval = self.nameBox.value
        self.nameBox.value = ''
        return retval

def get_events():
    events = pygame.event.get()
    mousepos = pygame.mouse.get_pos()
    notfore = True
    value = None
    for textinput in inputters:
        if pygame.rect.Rect(textinput.nameBox.x, textinput.nameBox.y, 100, 50).collidepoint(mousepos):
            textinput.run(events)
            notfore = False
    if notfore:
        return events
    else:
        return []

To clarify what it does:

  • the inputter object contains a text input from EzText. It is meant to output its contents to a global variable when the enter key is pressed.
  • The get_events() function gathers events, and, if the mouse is over the text input, sends them to that, if not, it returns the events to be processed in other ways.

It is entirely possible that I do not understand how EzText is meant to be used and am retrieving the value incorrectly.

Reblochon Masque
  • 33,202
  • 9
  • 48
  • 71
trevorKirkby
  • 1,896
  • 18
  • 46
  • Maybe explain was EzText actually is and where to find it – sloth May 14 '14 at 08:15
  • It looks to me like you will have to keep track of whether or not the text box is in focus or not by manually checking the mouse events, and then simply don't call `self.nameBox.update(events)` (in your run) unless it has focus. – KSab May 14 '14 at 09:16
  • That is what my code attempts to do. However, first of all this code does not work, and second of all it would be preferable to use a module that includes expanded widgets, as it will likely implement the text input in a better way. – trevorKirkby May 14 '14 at 17:37
  • EzText is a pygame library that allows you to create text inputs. You initialize an EzText Input object, and pass it the events from pygame.event.get() for it to update. – trevorKirkby May 14 '14 at 17:39

0 Answers0