Python의 Tkinter에서 마우스로 텍스트를 선택할 수 있도록 레이블을 어떻게 만들 수 있습니까?

StackOverflow https://stackoverflow.com/questions/1602106

  •  05-07-2019
  •  | 
  •  

문제

Python의 Tkinter 인터페이스에는 레이블에서 텍스트를 선택한 다음 클립 보드에 복사 할 수있는 레이블을 변경하는 구성 옵션이 있습니까?

편집하다:

이러한 기능을 제공하기 위해이 "Hello World"앱을 어떻게 수정 하시겠습니까?

from Tkinter import *

master = Tk()

w = Label(master, text="Hello, world!")
w.pack()

mainloop()
도움이 되었습니까?

해결책

가장 쉬운 방법은 높이가 1 라인 인 비활성화 된 텍스트 위젯을 사용하는 것입니다.

from Tkinter import *

master = Tk()

w = Text(master, height=1, borderwidth=0)
w.insert(1.0, "Hello, world!")
w.pack()

w.configure(state="disabled")

# if tkinter is 8.5 or above you'll want the selection background
# to appear like it does when the widget is activated
# comment this out for older versions of Tkinter
w.configure(inactiveselectbackground=w.cget("selectbackground"))

mainloop()

비슷한 방식으로 항목 위젯을 사용할 수 있습니다.

다른 팁

위 코드를 변경했습니다.

from tkinter import *

master = Tk()

w = Text(master, height=1)
w.insert(1.0, "Hello, world!")
w.pack()



# if tkinter is 8.5 or above you'll want the selection background
# to appear like it does when the widget is activated
# comment this out for older versions of Tkinter
w.configure(bg=master.cget('bg'), relief=FLAT)

w.configure(state="disabled")

mainloop()

디스플레이의 일반적인 부분처럼 보이려면 구호가 평평해야합니다. :)

어느 쪽이든 선택할 수있는 텍스트를 만들 수 있습니다 Text 또는 Entry텍스트를 사용하는 것이 정말 유용하다고 생각합니다. 정말 도움이 될 수 있습니다! 여기에 입력 코드를 보여줍니다.

from tkinter import *
root = Tk()
data_string = StringVar()
data_string.set("Hello World! But, Wait!!! You Can Select Me :)")
ent = Entry(root,textvariable=data_string,fg="black",bg="white",bd=0,state="readonly")
ent.pack()
root.mainloop()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top