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.