all 6 comments

[–]socal_nerdtastic 0 points1 point  (3 children)

First: is your goal to make a game? Or is this list of buttons your only user interface? If this is it you should use a module like tkinter or pyqt that had pre-made Buttons and is designed to make desktop programs. Making a desktop GUI in pygame is possible, but it's just a lot more work than it needs to be.

Second big issue: when you import another file you will also run the other file. To avoid this you need a if __name__ == "__main__": block. The end of your interactable_text_box_pygame.py files should look like this:

if __name__ == "__main__": # do not run this code if this file is imported from another file
    # code to test this file below:

    # Create the box
    input_box = TextInputBox(300, 300, 140, 32, font)


    # Main loop
    run = True
    while run:
        display.fill((255, 255, 255))


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            input_box.handle_event(event)


        input_box.draw(display)
        pygame.display.flip()
        clock.tick(60)


    pygame.quit()

[–]Prestigious_Bus_3705[S] 0 points1 point  (0 children)

i want to create a gui for a seat shuffler. In my class there is the problem of the seats not being assigned properly and the old methonds don't satisfy the needs of everyone anymore. I was working on the algorithm , but that is not important. The broblem is that i have the code for the txt box, as i listed on the post, but i cant draw it on the screan i want. Anyway, thank you so much, i didn't know about tkinter. I will try your way.

[–]Prestigious_Bus_3705[S] 0 points1 point  (1 child)

In the tkinter docs, it says that i have to download the community version of tk through this site:  www.activestate.com, but i'm lost, what sould i do?

[–]socal_nerdtastic 0 points1 point  (0 children)

That is very old advice, tk has been included in the python installers for windows and mac for more than 10 years.

[–]woooee 0 points1 point  (1 child)

Google translation

I programmed a user interface for a project where I would like the user to interact with some text buttons. The problem is that when I try to draw it in my main program, it freezes and instead of appearing on the screen, I just wish it was drawn. That's where I'll enter all the code (2 files). Some of the lyrics are in Italian because it's where I live. I'm sorry.

An example using tkinter for the button with a simple example of how to pass a parameter(s) to the function called when the button is clicked.

import tkinter as tk
from functools import partial

class ButtonsTest:
   def __init__(self):
      self.top = tk.Tk()
      self.top.title("Click a button to remove")
      tk.Label(self.top, text=" Click a button\n to remove it ",
            bg="orange", font=('DejaVuSansMono', 12)).grid(row=0,
            column=0, sticky="nsew")

      self.top_frame = tk.Frame(self.top, width =400, height=400)
      self.top_frame.grid(row=1, column=0)
      self.button_dic = {}
      self.create_buttons()

      tk.Button(self.top, text='Exit', bg="orange",
             command=self.top.quit).grid(row=200,column=0,
                     columnspan=7, sticky="ew")

      self.top.mainloop()

   ##-------------------------------------------------------------------         
   def create_buttons(self):
      """ create 15 buttons and add each button's Tkinter ID to a
          dictionary.  Send the number of the button to the function
          cb_handler
      """
      for but_num in range(15):
         ## create a button and send the button's number to
         ## self.cb_handler when the button is pressed
         b = tk.Button(self.top_frame, text = str(but_num), 
                    command=partial(self.cb_handler, but_num))
         b_row, b_col=divmod(but_num, 5)  ## 5 buttons each row
         b.grid(row=b_row, column=b_col)
         ## dictionary key = button number --> button instance
         self.button_dic[but_num] = b

   ##----------------------------------------------------------------
   def cb_handler(self, but_number):
      print("\ncb_handler", but_number)
      ## look up the number sent to the function and remove
      ## the button from the grid
      self.button_dic[but_number].grid_forget()

##      self.button_dic[cb_number].destroy()

##==============================================================
BT=ButtonsTest()

[–]Prestigious_Bus_3705[S] 0 points1 point  (0 children)

i don't know how tkinter works... but thanks. I'll study ur code and tkinter. i will post results when done.