如何在Tkinter中分别启用用于粘贴和复制的右键单击输入和输出小部件?

2024-06-23 18:51:00 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python字典的帮助下,我还在开发翻译器应用程序。但我有一个挑战:我希望能够右键单击entry小部件并粘贴键,以及右键单击output小部件并复制值。我只能用键盘快捷键;为了方便起见,我希望能够用鼠标来实现这一点。谢谢代码如下:

from tkinter import *
import tkinter. messagebox
root=Tk()
root.geometry('250x250')
root.title("Meta' Translator")
root.configure(background="#35424a")
from playsound import playsound

#Entry widget object
textin = StringVar()

#press ENTER key to activate translate button
def returnPressed(event):
  clk()

def clk():
    entered = ent.get().lower() #get user input and convert to lowercase
    output.delete(0.0,END)
    if len(entered) > 0:
        try:
            textin = exlist[entered]
        except:
            textin = 'Word not found'
        output.insert(0.0,textin)

def play():
    text = output.get("0.0", "end").strip("\n")
    if text == "əsɔ́":
        playsound("hoe.mp3")
    elif text == "jam":
        playsound("axe.mp3")
    elif text == "ɨghə́":
        playsound("eye.mp3")
    else:
        # If there is no sound file for the translation:
        playsound("eze.mp3")

#heading
lab0=Label(root,text='Translate English Words to Meta\'',bg="#35424a",fg="silver",font= 
('none 11 bold'))
lab0.place(x=0,y=2)

#Entry field
ent=Entry(root,width=15,font=('Times 18'),textvar=textin,bg='white')
ent.place(x=30,y=30)

#focus on entry widget
ent.focus()

#Search button
but=Button(root,padx=1,pady=1,text='Translate',command=clk,bg='powder blue',font=('none 18 
bold'))
but.place(x=60,y=90)

#press ENTER key to activate Translate button
root.bind('<Return>', returnPressed)

#output field
output=Text(root,width=15,height=1,font=('Times 18'),fg="black")
output.place(x=30,y=170)

#play button
play_button=Button(root,padx=1,pady=1,text='Play',command=play,bg='powder blue',font=('none 
10 bold'))
play_button.place(x=100,y=210)

#prevent sizing of window
root.resizable(False,False) 

#Dictionary
exlist={
    "hat":"ɨ̀də̀m", 
    "hoe":"əsɔ́",
    "honey":"jú",
    "chest":"ɨgɔ̂",
    "eye":"ɨghə́",
    "ear":"ǝ̀tǒŋ",
    "axe":"jam"

    }

root.mainloop()

Tags: totextimportoutputplayplacebuttonroot
1条回答
网友
1楼 · 发布于 2024-06-23 18:51:00

由于您的代码有很多依赖项,因此无法在其他系统上运行,因此下面是一个常见的示例,您应该能够轻松地将其实现到您的代码中:

from tkinter import *

root = Tk()

def popup(event):
    try:
        menu.tk_popup(event.x_root,event.y_root) # Pop the menu up in the given coordinates
    finally:
        menu.grab_release() # Release it once an option is selected

def paste():
    clipboard = root.clipboard_get() # Get the copied item from system clipboard
    e.insert('end',clipboard) # Insert the item into the entry widget

def copy():
    inp = e.get() # Get the text inside entry widget
    root.clipboard_clear() # Clear the tkinter clipboard
    root.clipboard_append(inp) # Append to system clipboard

menu = Menu(root,tearoff=0) # Create a menu
menu.add_command(label='Copy',command=copy) # Create labels and commands
menu.add_command(label='Paste',command=paste)

e = Entry(root) # Create an entry
e.pack(padx=10,pady=10)

e.bind('<Button-3>',popup) # Bind a func to right click

root.mainloop()

我已经解释了它与评论,以了解在进行中,没有什么复杂的。当您右键单击条目时,菜单会弹出,因为函数仅绑定到条目。我认为,如果没有clipboard_clear(),它会将tkinter剪贴板中的所有项目附加到系统剪贴板

相关问题 更多 >

    热门问题