简体   繁体   中英

How to copy input from entry field and copy to clipboard with Tkninter and Python?

Try to copy to clipboard with Tkninter from the entry input field. For example, write in the entry, set to str, press button to copy to clipboard. Every time I push the button I get None printed to console and the window closes.

from tkinter import * 


root = Tk()

e = Entry(root)
e.pack()
newText = e.get() 

class Main(object):
   
 
def ThisWorks():
    print(e.get())

def callback(text=newText): 
    root.withdraw()       
    root.clipboard_clear
    root.clipboard_append(newText)
    print(root.clipboard_append(newText))

b = Button(root, text="test", width=10, command=callback)
b.pack()
b2 = Button(root, text="test2", width=10, command=ThisWorks)
b2.pack()


butquit = Button(root, text="exit", command=root.quit)
butquit.pack()

root.mainloop()

You have to call e.get() inside callback . newText only has the value at the time you called e.get() , and you called it about a millisecond after creating the entry widget. The user won't even have seen the widget, much less had a chance to enter any data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM