Mr Jos Posted October 17, 2021 Posted October 17, 2021 I'm making a keyboard to make a new name for a box that will be stored in my warehouse. Each added letter will be stored in a list, and then a string is made from all these letters/numbers/spaces.. I'm trying to get the pixel size (width) of that string each time something is added to see if it still fits on the screen, if not it will delete the last added again. I have been trying to get the text_width working, but I don't understand what I'm doing wrong as I only get errors. Information at https://pybricks.com/ev3-micropython/media.html#module-pybricks.media.ev3dev ev3 = EV3Brick() small_font = Font(size=6) # 6 pixel height for text on screen ev3.screen.set_font(small_font) #Choose a preset font for writing next texts ev3.screen.clear() #Make the screen empty (all pixels white) print(ev3.screen.text_width("lbg 13l")) ev3.screen.draw_text(72, 15, "lbg 13l", text_color=Color.BLACK, background_color=Color.WHITE) AttributeError: 'Image' object has no attribute 'text_width' I was expecting to have an amount of pixels to be shown as an integer in the Output box from Visual Studio Code. If I get that working I can use it in the real coding for the namegiving. Quote
David Lechner Posted October 17, 2021 Posted October 17, 2021 (edited) text_width() is a method on the font object, not the screen/image object. so width = small_font.text_width(text) Edited October 17, 2021 by David Lechner Quote
Mr Jos Posted October 18, 2021 Author Posted October 18, 2021 21 hours ago, David Lechner said: width = small_font.text_width(text) Thanks! for x in temp_name_list: temp_name += x if small_font.text_width(temp_name) > max_length_screenname: temp_name_list.pop() Works perfect, now I can finish my HMI and start real testing soon. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.