copy text from pygame

Asked

Viewed 52 times

-1

Guys, I’ve tried everything and I can’t copy a text from a Pygame window. The code is as follows:

import pygame
screen = pygame.display.set_mode((800, 600))
pygame.font.init()
font1 = pygame.font.Font(None, 150)
text = font1.render("Alô Mundo", True, (255, 255, 255))
screen.blit(text, (0, 200))
pygame.display.flip()

def principal():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
               return
        pygame.time.delay(30)

principal()

I would like to copy the Alo Mundo that will appear on the pygame screen. Anyone have any suggestions ?

  • what did you mean by copiar ?

  • be able to hover over the text and copy it, as is usually the case with any text

  • I understand it is not possible, because the text is rendered as an image. If possible, it will surprise me. Someone?

  • exact, you know if it is possible with Tkinter ?

  • Yes, "printing" the text as label I believe it is possible.

  • but there is no way to copy the text of a label, it has ?

  • Replying in the post

Show 2 more comments

1 answer

0

Based on what we talked about in the comments.

Pygame

In the pygame I don’t think it’s possible...

Tkinter

Is there any way to copy the label in the tkinter, however nay is the standard.

See the code below:

from tkinter import *

master = Tk()

w = Text(master, height=1, borderwidth=0)
w.insert(1.0, "Copia este texto!!!")
w.pack()
w.configure(state="normal")  # o default de state é `disable`
mainloop()

Update

All people point to a text entry. However if you really want to do with Label, although not intuitive, you can create a function that captures text when the label is clicked.

See below

from tkinter import *
master = Tk()

def pega_texto(event):
    lbl = event.widget
    master.clipboard_clear()
    master.clipboard_append(lbl.cget("text"))
    master.update()  # mantém no clipboard mesmo fechandpo a janela.


lbl = "Hello World!"
w = Label(master, text=lbl)
w.bind("<Button-1>", pega_texto)
w.pack()

mainloop()

I hope it helps

  • but this is already a text widget, there is no way to copy the label ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.