Bitte warten...

GTK+ 3: Popup-Dialoge

Gtk.Dialog

► GTK+ 3 Dokumentation: Gtk.Dialog
► Python GTK+ 3 Tutorial: Dialogs

Hier zunächst ein einfaches Beispiel, wie man einen Popup-Dialog erzeugt, und wie die Ereignisse beim Anklicken des Abbrechen- bzw. OK-Buttons verarbeitet werden:

Code kopieren
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

class MainWindow(Gtk.Window):

    def __init__(self):
      
        Gtk.Window.__init__(self, title="Fenster mit Popup-Dialog")
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_default_size(300, 300)
        self.set_border_width(10)

        self.box01 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, homogeneous=False, spacing=5)
        self.add(self.box01)

        self.toolbutton01 = Gtk.ToolButton.new_from_stock("gtk-edit")
        self.toolbutton01.connect('clicked', self.on_toolbutton01_clicked)  # Dies löst den Popup-Dialog aus
        self.box01.pack_start(self.toolbutton01, False, False, 0)
        
        self.show_all()

    def on_toolbutton01_clicked(self, widget):
        dialog = DialogEntry(self)
        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            # Reaktion auf OK-Button
            print("Eingabe:", dialog.entry.get_text())
        elif response == Gtk.ResponseType.CANCEL:
            # Reaktion auf Abbrechen-Button
            print("Abbruch")

        dialog.destroy()

class DialogEntry(Gtk.Dialog):  # Die Klasse für das eigentliche Popup-Fenster

    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "Popup-Dialog", parent, 0,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))

        self.set_border_width(10)
        self.box = self.get_content_area()
        self.label = Gtk.Label("Geben Sie einen Text ein:")
        self.box.add(self.label)
        self.entry = Gtk.Entry()
        self.box.add(self.entry)
        self.show_all()

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