22

Python の tkinter インターフェイスで、Label 内のテキストを選択してクリップボードにコピーできるように、Label を変更する構成オプションはありますか?

編集:

このような機能を提供するために、この "hello world" アプリをどのように変更しますか?

from Tkinter import *

master = Tk()

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

mainloop()
4

5 に答える 5

15

最も簡単な方法は、高さが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()

同様の方法でエントリウィジェットを使用できます。

于 2009-10-21T17:26:49.013 に答える
5

上記のコードにいくつかの変更を加えました。

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()

レリーフをディスプレイの通常の部分のように見せるためには、レリーフを平らにする必要があります。:)

于 2010-09-26T21:33:59.333 に答える
0

The other answers insert text to the text box instead of replacing the text. That works when you need to change the text only for one time. However, you need to delete the line first if you need to replace it. The following code would fix this issue:

from tkinter import *

master = Tk()

w = Text(master, height=1)
w.delete(1.0, "end")
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()
于 2021-06-17T19:51:28.697 に答える