简体   繁体   中英

Bind Enter key to Button (PyGObject)

I'm writing a very simple GUI program for educational purposes. The window has a Gtk.Entry area, from which I grab the input and use as argument for the function num_check(), which checks whether the number is odd or even (and outputs an error if the input is invalid).

The button works fine, but I want to be able to use the Enter key instead of the mouse. And after pressing Enter, the focus should return to the input box (Gtk.Entry)

Here's the code. I appreciate any help.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
import odd_even

class EntryWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="ODD OR EVEN")
        self.set_size_request(200, 100)

        self.timeout_id = None

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        self.entry = Gtk.Entry()
        self.entry.get_text()
        vbox.pack_start(self.entry, True, True, 0)

        button = Gtk.Button.new_with_label("Go")
        button.connect("clicked", self.on_click_me_clicked)
        vbox.pack_start(button, True, True, 0)

        self.label = Gtk.Label()
        self.label.get_text()
        vbox.pack_start(self.label, True, True, 0)

# after pressing "Go", grab text from entry and run num_check() with it, then set label with the result
    def on_click_me_clicked(self, button):
        number = self.entry.get_text()
        func = odd_even.num_check(number)
        print(func)

        self.label.set_text(func)


win = EntryWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Try this code:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
import odd_even

class EntryWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="ODD OR EVEN")
        self.set_size_request(200, 100)

        self.timeout_id = None

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(vbox)

        self.entry = Gtk.Entry()
        self.entry.get_text()
        self.entry.connect("activate", self.on_entry_activate)
        vbox.pack_start(self.entry, True, True, 0)

        button = Gtk.Button.new_with_label("Go")
        button.connect("clicked", self.on_click_me_clicked)
        vbox.pack_start(button, True, True, 0)

        self.label = Gtk.Label()
        self.label.get_text()
        vbox.pack_start(self.label, True, True, 0)

    def on_entry_activate (self, entry):
        number = entry.get_text()
        func = odd_even.num_check(number)
        print(func)

        self.label.set_text(func)

# after pressing "Go", grab text from entry and run num_check() with it, then set label with the result
    def on_click_me_clicked(self, button):
        number = self.entry.get_text()
        func = odd_even.num_check(number)
        print(func)

        self.label.set_text(func)


win = EntryWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Edit: the docs for the signals are here .

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