Python / Pygame tutorial: Getting text input

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ธ.ค. 2024

ความคิดเห็น • 139

  • @HiVikas
    @HiVikas 4 ปีที่แล้ว +81

    3:47 "Now we can see that cant see anything"
    - Clear Code

    • @py-gamejam8025
      @py-gamejam8025 4 ปีที่แล้ว +10

      4:09 "You still can't see anything"

  • @shourya9998
    @shourya9998 4 ปีที่แล้ว +12

    This guy is a gifted teacher!
    I pray for your healthy life

  • @chingkwanwong4035
    @chingkwanwong4035 2 ปีที่แล้ว +2

    The explanation is clear and concise ( but 1.25 times is better). A really helpful video.

  • @adarshkhatri993
    @adarshkhatri993 3 ปีที่แล้ว +1

    Bro..bro.. bro.....
    I can't thank you in words, bro you are so awesome and I solved all my doubts

  • @eannaoreilly7244
    @eannaoreilly7244 3 ปีที่แล้ว

    thank you i couldnt find anyone adressing except you for my project thankyou

  • @scubing9486
    @scubing9486 4 ปีที่แล้ว +1

    Good man as expected explained very very very well

  • @dogushangunduzer
    @dogushangunduzer ปีที่แล้ว

    Such a clean lecture, master at work, cheers.

  • @gortilla
    @gortilla 2 ปีที่แล้ว +4

    if your backspace doesn't work try adding " clear_rect = pygame.Rect(left position, top position, width, height) " by your input_rect declaration and " pygame.draw.rect(screen, white, clear_rect) " beneath your "pygame.draw.rect(screen, color, input_rect, 2)"

  • @jagobot1487
    @jagobot1487 13 วันที่ผ่านมา

    Genuinely, thank you so much

  • @andreslarrondobuno7630
    @andreslarrondobuno7630 2 ปีที่แล้ว

    you explain things so clearly holy shit, thanks

  • @aramihovygimenezzarate6497
    @aramihovygimenezzarate6497 4 ปีที่แล้ว +1

    Congratulations! Its super clear

  • @omkakatkar9003
    @omkakatkar9003 4 ปีที่แล้ว

    Thanks for the tutorial. It really helped. You deserve more likes

  • @autisticcondom7982
    @autisticcondom7982 4 ปีที่แล้ว +4

    how can i do it in the way that after writing the text in pygame and pressing enter the input is saved and checked by the program to see if its correct or not

  • @shourya9998
    @shourya9998 4 ปีที่แล้ว

    This channel is insane!
    Subscibed!!!

  • @wesleyhaw1806
    @wesleyhaw1806 2 ปีที่แล้ว

    Thanks for the video, it helped me a lot!

  • @36nishantchandeleceb25
    @36nishantchandeleceb25 2 ปีที่แล้ว +1

    Here to add a little bit of extra info
    If for some reason your new text is overlapping with your previous text
    Add a line
    Screen.fill((0,0,0))
    After user_text=user_text[:-1]

  • @potatokiller8638
    @potatokiller8638 4 ปีที่แล้ว +10

    how i can down a line, when i press enter?

    • @nguyendangminh9433
      @nguyendangminh9433 4 ปีที่แล้ว +2

      use K_RETURN

    • @shourya9998
      @shourya9998 4 ปีที่แล้ว +1

      if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_RETURN:
      YOURVAR += '
      '

    • @coolfred9083
      @coolfred9083 3 ปีที่แล้ว

      @@shourya9998 I don't think that works, it just makes a missing character which I assume represents _new line_ instead of actually making a new line

    • @bigboibob2568
      @bigboibob2568 3 ปีที่แล้ว

      Not entirely sure this works nor have I tried it myself yet and I'm probably late anyway but could you print the text to the screen then do the coordinates that the text renders to -1 on the y axis

    • @shourya9998
      @shourya9998 3 ปีที่แล้ว

      @@bigboibob2568 Yeah that doesn't work it just prints a block, my bad :(

  • @sylvanfranklin6904
    @sylvanfranklin6904 3 ปีที่แล้ว

    Thanks so much for your tutorials

  • @BrandonJacobson
    @BrandonJacobson 4 ปีที่แล้ว

    Great tutorial! I'm building a Pygame on my TH-cam channel and I'll be using this. Thanks again!

  • @jaxonwooton2757
    @jaxonwooton2757 2 ปีที่แล้ว +1

    When I press backspace it just moves back a letter instead of deleting the last one. So when I clicks another key it writes over other text that hasn’t been deleted. Help

  • @youtuberecommended9183
    @youtuberecommended9183 4 ปีที่แล้ว

    Clear and concise👌🏾

  • @senelekiz8979
    @senelekiz8979 2 ปีที่แล้ว

    Very helpful for me. Thank you!

  • @michaelsaspri4996
    @michaelsaspri4996 2 ปีที่แล้ว

    Thanks so much just what I needed ..

  • @soupnoodles
    @soupnoodles 3 ปีที่แล้ว +21

    It would be nice to provide the source code in the description

    • @trace6585
      @trace6585 2 ปีที่แล้ว +10

      You're supposed to be here to learn, not copy and paste.

  • @Eastcoast_Rds
    @Eastcoast_Rds ปีที่แล้ว

    great tutorial, thank you!

  • @fatihhamza3259
    @fatihhamza3259 4 ปีที่แล้ว +3

    thaank you so much ❤❤

  • @notnanook
    @notnanook 4 ปีที่แล้ว

    Very well made video 👌

  • @ХуанМатус-х8ю
    @ХуанМатус-х8ю 2 ปีที่แล้ว

    Thank you from Russia

  • @Anonym-qe2di
    @Anonym-qe2di 4 ปีที่แล้ว +1

    Awesome! Keep going!

  • @cragkarottensalat2293
    @cragkarottensalat2293 3 ปีที่แล้ว +2

    How can I convert it into a function?
    because I have real problems with it

  • @ananyatandon6536
    @ananyatandon6536 3 ปีที่แล้ว

    THANK YOU SO MUCH

  • @goofvos
    @goofvos 2 ปีที่แล้ว +1

    My backspace is not working. If I type something and I press backspace, the characters do not go away, and if i type something again, it overlaps the existing characters

    • @ivancrescenzi7194
      @ivancrescenzi7194 ปีที่แล้ว

      I don't know if you still need it, but the problem is that screen.blit(user_text etc ) keeps writing the text above the previous one, so you don't notice that when you use event.unicode, since the program keeps writing the same string you can roughly cover the previous one by drawing a smaller and fully coloured rectangle inside the one that you already have

    • @hürriyetpalestine
      @hürriyetpalestine 8 หลายเดือนก่อน

      double check your code with his. I did it just now, and it works fine.

  • @RETROGAMER-c1t
    @RETROGAMER-c1t 10 หลายเดือนก่อน

    this is nice and all but what about the enter input?

  • @not_a_pythonhehim535
    @not_a_pythonhehim535 11 หลายเดือนก่อน

    I was hoping this would give me a way to use the text as an input

  • @simplepycodes
    @simplepycodes 4 ปีที่แล้ว +1

    If i could give you 10 thumbs up i would do it :). very good tutorial.

  • @carmona9835
    @carmona9835 2 ปีที่แล้ว

    very good tutorial

  • @Raphy084
    @Raphy084 3 ปีที่แล้ว

    I'm not new with python and pygame but it didn't came to my mind to use "max" for the width of rect.
    Why I'm always forgetting "max" and "min" function?

  • @dhruveshgangapuram5755
    @dhruveshgangapuram5755 3 ปีที่แล้ว

    nice way of explaination

  • @dawe7081
    @dawe7081 2 ปีที่แล้ว +1

    this is awesome but when I paste something into it, only a rectange appears. how do I fix this?

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      what do you mean by the word
      'paste' ?

    • @dawe7081
      @dawe7081 2 ปีที่แล้ว

      @@TutorialTube448 ctrl+v

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      @@dawe7081 no i know that. I mean what are you trying to paste

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      And what are you trying to paste it into?

    • @dawe7081
      @dawe7081 2 ปีที่แล้ว

      @@TutorialTube448 any text. for example I copy "Hello World", I past it and it shows a rectangle. (i am pasting this on the window. I would imagine this should work as if you wrote the entire text you are trying to paste as if you typed it word by word)

  • @cutiemaltesegucci
    @cutiemaltesegucci 4 ปีที่แล้ว

    How can I make a code that other language letters can be entered? not only the English alphabet but also Korean, Japanese, Chinese

  • @matthewpolitis5119
    @matthewpolitis5119 3 ปีที่แล้ว

    Hi, It is not stopping how many characters/width is allowed. How can I fix this?

  • @Arpit_Arora
    @Arpit_Arora 3 ปีที่แล้ว

    very nice...to the point

  • @ancientgear7192
    @ancientgear7192 3 ปีที่แล้ว +1

    Hello. Can someone help me? How do we set boundaries so that when we type a huge line the box will reshape and the line wont go out of the screen?

    • @ClearCode
      @ClearCode  3 ปีที่แล้ว +1

      adding an if statement in there should do the trick quite easily

  • @jeniswinsta6196
    @jeniswinsta6196 2 ปีที่แล้ว

    Thanks very helpful

  • @Arpit_Arora
    @Arpit_Arora 3 ปีที่แล้ว

    Thank you so much sir...

  • @theyoungcoder7239
    @theyoungcoder7239 2 ปีที่แล้ว

    Which code editor do you use? Pls tell

  • @ankushsarkar1746
    @ankushsarkar1746 4 ปีที่แล้ว

    Like ur vibe man

  • @joaleo6318
    @joaleo6318 4 ปีที่แล้ว +1

    Nice vid ! But I want to "ask" the user to type his username and print it while my game is playing but I don't know how to do it ... help ?

    • @abdullahalawad4906
      @abdullahalawad4906 4 ปีที่แล้ว

      If you want to make this while the game is running, you can make a rectangle at the bottom asking for the name and stop all inputs of moving the player and then do the steps in the video.
      I guess this should work anyway

    • @Ashersminicartoons
      @Ashersminicartoons 4 ปีที่แล้ว +1

      You can define a new loop at the end of your program and add this code inside, also add this: if event.key == pygame.K_RETURN:
      global i
      i = user_text
      main() # Your loop's name might be different
      Then blit the text as the variable i at the start of your main (or other) loop.

    • @Ashersminicartoons
      @Ashersminicartoons 4 ปีที่แล้ว

      and also instead of calling the main loop at the bottom call the new loop

  • @akramelomrani8728
    @akramelomrani8728 4 ปีที่แล้ว

    a question please if i just want to have the same with the same width but when the char are too long the just go in it

    • @zerokun2655
      @zerokun2655 4 ปีที่แล้ว

      i don't know, try drawing a black rectangle on top of the righ side of the text box that goes to the and of the screen, the effect will be the same

  • @patriciofernandez6500
    @patriciofernandez6500 6 หลายเดือนก่อน

    So it's impossible to write kanji, or a word with a vowel with accent like León. 😢

  • @divyanshsharma3519
    @divyanshsharma3519 3 ปีที่แล้ว

    Hi is there any way I can save the user input in a variable, and then use it to dispaly further in my program?

    • @benoitkinziki3916
      @benoitkinziki3916 3 ปีที่แล้ว

      Isn't that exactly what he did with his user_input variable

  • @RohanKumar-zd3pc
    @RohanKumar-zd3pc 4 ปีที่แล้ว

    Clear code's the best

  • @adlifiruz808
    @adlifiruz808 3 ปีที่แล้ว +1

    how do you apply this into classes?

  • @programmertank3029
    @programmertank3029 3 ปีที่แล้ว

    I don't have the 'l' when I run the code

  • @whack9730
    @whack9730 3 ปีที่แล้ว

    It is saying clock not defined.
    Is there any way i can fix this

    • @ClearCode
      @ClearCode  3 ปีที่แล้ว

      define a clock? :P

  • @aadilidreesi3819
    @aadilidreesi3819 4 ปีที่แล้ว

    I am a learner and i just started coding,
    i am getting an error that
    (name += event.unicode
    TypeError: unsupported operand type(s) for +=: 'pygame.Surface' and 'str')

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว +1

      You seem to be trying to add a surface to the text, check the line where you render the text, I would guess a wrong variable ended up in there.

    • @aadilidreesi3819
      @aadilidreesi3819 4 ปีที่แล้ว

      @@ClearCode thanks, i caught my error ,i used same variable for rendering

  • @kaan_tuna
    @kaan_tuna 2 ปีที่แล้ว

    How I did this before watching this (I still have it like this) is I wrote lines of code to get text input for every single letter, numbers and symbols because I didn't realize event.unicode was a function. And it is a success so I don't want to waste it now -_-

  • @hajp
    @hajp 3 ปีที่แล้ว

    How to limit the character count? can you explain on it example? i used this ccde and my rectangle is infinite

    • @ClearCode
      @ClearCode  3 ปีที่แล้ว

      just add an if statement that measures the length of the string?

    • @hajp
      @hajp 3 ปีที่แล้ว

      @@ClearCode can you please write it for me :( im a beginner and i just rewrote the code

    • @ClearCode
      @ClearCode  3 ปีที่แล้ว +3

      @@hajp around minute 5 on line 15 when it says user_text += event.unicode put it in the if statement -> if len(user_text) < 10

    • @ClearCode
      @ClearCode  3 ปีที่แล้ว

      I just made up the number 10, chose whatever amount of characters you want

  • @aggelosn21
    @aggelosn21 4 ปีที่แล้ว

    good job mate

  • @Rajsingh-rs1yi
    @Rajsingh-rs1yi 4 ปีที่แล้ว

    Does anyone know how to make a slider in pygame using classes? I am really struggling with this.

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      you don't have to believe but pygame is a great module in making sliders, however tkinter (module that you don't need to download in anything because it is part of python) can do a great job in making a slider]
      Widgets are various controls, such as buttons, labels, scrollbars, radio buttons, and text boxes used in a GUI application. They are also little components or controls of Graphical User Interface (GUI).
      Pady Puts some space between the button widgets and the borders of the frame and the borders of the root window
      Pack Is a geometry manager that just basically stacks widgets on top of each other.
      TKINTER CODE FOR MAKING SLIDERS
      slider_frame = tk.Frame(root, borderwidth=5, relief=tk.SUNKEN) # --
      slider_frame.pack(pady=10)
      slider = tk.Scale(slider_frame,
      from_=0.00,# starting point of the range for sliding from.(minimum)
      to=1.0, and this is the max in the range
      orient=tk.VERTICAL, for the way it is going to be seen as
      length=400, -- how long the widget is
      tickinterval = 0.1,
      resolution = 0.01,

      slider.pack()

  • @glitchneon8419
    @glitchneon8419 4 ปีที่แล้ว

    I keep getting the error: line 11, in
    if event.type == pygame.QUIT():
    TypeError: 'int' object is not callable

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว

      There shouldn't be parenthesis after QUIT, it should be:
      if event.type == pygame.QUIT:

    • @glitchneon8419
      @glitchneon8419 4 ปีที่แล้ว

      @@ClearCode ohhhhh, thanks!

    • @glitchneon8419
      @glitchneon8419 4 ปีที่แล้ว

      ​@@ClearCode when I did that, it said: line 16, in
      text_surface = base_font.pygame.render(user_text,True,(255,255,255))
      AttributeError: 'pygame.font.Font' object has no attribute 'pygame'

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว

      There should not be pygame after the base_font. So just: base_font.render

  • @gabrielbatista5433
    @gabrielbatista5433 4 ปีที่แล้ว

    Hey
    I'm having a problem with the colors:
    "color_active = pygame.color('lightskyblue3')
    TypeError: 'module' object is not callable"
    Can anyone help me?

    • @heyohola3143
      @heyohola3143 4 ปีที่แล้ว +2

      Hi, try this:
      color_active = pygame.Color('lightskyblue3')
      It looks like there is a syntax error. The letter "c" in "pygame.color" has to be uppercase. hope this helps

    • @umanglunia2194
      @umanglunia2194 4 ปีที่แล้ว +2

      pygame.Color not color

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      this is a different way than what you know but it works if you put it in a variable
      R G B
      0, 128, 255 = Lblue
      0 , 0 , 255 = blue
      0 , 0 , 128 = dblue

  • @emeraldthunder
    @emeraldthunder 3 ปีที่แล้ว

    Thank you.

  • @Ashersminicartoons
    @Ashersminicartoons 4 ปีที่แล้ว

    I want to put a limit to the characters, does anyone know how to do that??
    Also I really need help with this: if input_rect.collidepoint(event.pos):
    It is giving me this error:
    AttributeError: 'tuple' object has no attribute 'collidepoint'

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว +1

      for the character limit, go with something like
      if len(user_string) < char_limit: then add the user string
      and for the error below, it seems that your input_rect variable does not have a rectangle but a tuple, check what you put inside of that.

    • @Ashersminicartoons
      @Ashersminicartoons 4 ปีที่แล้ว +1

      @@ClearCode Thanks so much, I figured it out. I put active = False when the rect is more than 250 pixels wide so you can't expand the rect more and you cant type, also I figured out the rect tuple problem, I put input_rect = (WIDTH/2 - 130 , 500, 140, 32) instead of input_rect = pygame.Rect(WIDTH/2 - 130 , 500, 140, 32)

    • @Ashersminicartoons
      @Ashersminicartoons 4 ปีที่แล้ว

      WIDTH was equal to my screen width

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว

      @@Ashersminicartoons glad it worked out for you :) Although, do be aware that different characters usually have different widths, so your approach wouldn't limit the character count consistently, just be aware of that.

    • @RohanKumar-zd3pc
      @RohanKumar-zd3pc 4 ปีที่แล้ว

      You can use the assert statement as
      try:
      assert len(user_text) < (limit) :
      pass
      except AssertionError:
      print("Limit exceeded")
      This is the other way to add limitations.

  • @Schachlover
    @Schachlover 4 ปีที่แล้ว

    Thanks a lot.

  • @Rajsingh-rs1yi
    @Rajsingh-rs1yi 4 ปีที่แล้ว

    Can you make a tutorial for this using OOP please

    • @Martin-hj4re
      @Martin-hj4re 4 ปีที่แล้ว

      python isnt ment for object oriented programming.
      For this there are languages like Java or C

    • @deathshot9310
      @deathshot9310 3 ปีที่แล้ว +1

      @@Martin-hj4re it is but you can just choose not to

    • @ralfwright
      @ralfwright 2 ปีที่แล้ว +1

      C and OOP? 😂You mean C++?

  • @jsmcodes
    @jsmcodes 3 ปีที่แล้ว

    What if there are 2 inputs?

  • @harigopaladamus9481
    @harigopaladamus9481 9 หลายเดือนก่อน

    thanks brother haha

  • @akramelomrani8728
    @akramelomrani8728 4 ปีที่แล้ว

    you are awesome

  • @turtle8558
    @turtle8558 3 ปีที่แล้ว +1

    Ahh yes "now we can see that we can't see anything"

  • @quangdungtran2005
    @quangdungtran2005 2 ปีที่แล้ว

    👍👍👍👍👍👍

  • @priyanshithoughts7818
    @priyanshithoughts7818 3 ปีที่แล้ว

    Amazing

  • @harshitaramchandani5071
    @harshitaramchandani5071 4 ปีที่แล้ว

    no matter which key I press it displays only 'L'

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      it is not the code it is your laptop if you followed everything without an mistake.

  • @autisticcondom7982
    @autisticcondom7982 4 ปีที่แล้ว

    module 'pygame' has no attribute 'render

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว +1

      you need to render the font, not pygame itself

    • @autisticcondom7982
      @autisticcondom7982 4 ปีที่แล้ว

      @@ClearCode thank you

  • @yyhhttcccyyhhttccc6694
    @yyhhttcccyyhhttccc6694 13 วันที่ผ่านมา

    tysm

  • @chaowingchinghongfingshong7880
    @chaowingchinghongfingshong7880 3 ปีที่แล้ว

    can somebody send me the code?

  • @soltaneloudhabachi4648
    @soltaneloudhabachi4648 4 ปีที่แล้ว

    perfect

  • @Azurath100
    @Azurath100 2 ปีที่แล้ว

    stay in the box:
    if surface1.get_width() - 10 > input_rect.width:
    user_txt = user_txt[:-1]

  • @RohanKumar-zd3pc
    @RohanKumar-zd3pc 4 ปีที่แล้ว

    what does the flip function do?

    • @ClearCode
      @ClearCode  4 ปีที่แล้ว

      It reflects a surface

    • @RohanKumar-zd3pc
      @RohanKumar-zd3pc 4 ปีที่แล้ว

      @@ClearCode ok, Thanks for the amazing video

  • @marrlowne5884
    @marrlowne5884 3 ปีที่แล้ว

    "hi every butt"

  • @scpf2360
    @scpf2360 ปีที่แล้ว

    WOW

  • @Martinko1006
    @Martinko1006 7 หลายเดือนก่อน

    Delete key types random character

  • @apurvakumardani172
    @apurvakumardani172 3 ปีที่แล้ว

    I really think pygame should have some gui widgets.... Else the code gets messed up whipe we make our own widgets..

    • @TutorialTube448
      @TutorialTube448 2 ปีที่แล้ว

      is that just trying to make your widgets in pygame? and no other module?

  • @gummybear92
    @gummybear92 2 ปีที่แล้ว

    How do you make the text box understand
    ?