简体   繁体   中英

Gtk does not save text to the clipboard if I close the app right after

I'm new to Gnome development.

I am trying do create a button which, when pressed, will copy the content of its label to the clipboard and closes the app.

This is my code, where "self" is a class that extends Gtk.Window.

If I don't close the app, it works just fine.

Thank you

 ​    ​def​ ​copy_and_quit​(​self​, ​button​: ​Gtk​.​Button​): 
 ​        ​clip​ ​=​ ​Gtk​.​Clipboard​.​get​(​Gdk​.​SELECTION_CLIPBOARD​) 
 ​        ​clip​.​set_text​(​button​.​get_label​(), ​-​1​) 
 ​        ​self​.​close​()

Edit: I am a noob in Gtk development. I re-factorized my app to be a Gtk.Application with a Gtk.Window, following this example.

Now I do

 ​    ​def​ ​copy_and_quit​(​self​, ​button​: ​Gtk​.​Button​): 
 ​        ​clip​ ​=​ ​Gtk​.​Clipboard​.​get​(​Gdk​.​SELECTION_CLIPBOARD​) 
 ​        ​clip​.​set_text​(​button​.​get_label​(), ​-​1​) 
         Gtk.main_quit()

Which works.

Bye

By default, the clipboard content is stored in the app and is only copied to another app when you paste. This means when the program exits, the clipboard content is no longer available.

gtk_clipboard_store() stores the clipboard content outside the application to avoid this problem. GtkApplication calls it automatically when it shuts down, but if you're not using GtkApplication you need to call it manually.

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