User Tools

Site Tools


python:gtkgui

Table of Contents

python3 i gtk3

Kod w pythonie:

pygtk.py
from gi.repository import Gtk
 
class Handler:
    def __init__(self, builder):
        self.builder = builder
        self.textview1 = builder.get_object('textview1')
        self.textbuf = self.textview1.get_buffer()
 
    def window1_delete_event_cb(self, *args):
        Gtk.main_quit(*args)
 
    def button1_clicked_cb(self, button):
        self.textbuf.insert(self.textbuf.get_end_iter(), 'ala ma kota')
 
 
    def button2_clicked_cb(self, button):
        self.textbuf.set_text('')
 
    def button3_clicked_cb(self, button):
        Gtk.main_quit()
 
builder = Gtk.Builder()
builder.add_from_file('gui.glade')
builder.connect_signals(Handler(builder))
 
window = builder.get_object('window1')
window.show_all()
 
Gtk.main()

Kod (gui) wygenerowany przez glade:

gui.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Testowe okno</property>
    <signal name="delete-event" handler="window1_delete_event_cb" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkScrolledWindow" id="scrolledwindow1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hscrollbar_policy">never</property>
            <property name="shadow_type">in</property>
            <property name="min_content_height">1</property>
            <child>
              <object class="GtkTextView" id="textview1">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="hscroll_policy">natural</property>
                <property name="wrap_mode">char</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkButton" id="button1">
                <property name="label" translatable="yes">dodaj tekst</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="button1_clicked_cb" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label" translatable="yes">wyczyść</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="button2_clicked_cb" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button3">
                <property name="label" translatable="yes">koniec</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="button3_clicked_cb" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">2</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Screeny

Linki

python/gtkgui.txt · Last modified: 2016/12/11 11:16 by dmz