附件文件
gtkCheckBox.glade
gtkCheckBox.py
试验目的
Glade CheckButton(CheckBox)控件的使用与Python的事件绑定
试验过程
打开 Glade
创建窗体
名称:MainWindow
标题:Python GUI
创建表格控件(GtkTable)
所有属性默认
创建 CheckButton控件(GtkCheckButton)
名称: checkbutton1
创建CheckButton控件(GtkCheckButton)
名称: checkbutton2
定义 Glade 事件
窗体 destroy 事件
按键 clicked 事件
<?xml version="1.0"?> <glade-interface> <!-- interface-requires gtk+ 2.16 --> <!-- interface-naming-policy project-wide --> <widget class="GtkWindow" id="MainWindow"> <property name="title" translatable="yes">Python GUI</property> <property name="window_position">center</property> <property name="default_width">440</property> <property name="default_height">250</property> <signal name="destroy" handler="on_MainWindow_destroy"/> <child> <widget class="GtkVBox" id="vbox1"> <property name="visible">True</property> <property name="orientation">vertical</property> <child> <widget class="GtkCheckButton" id="checkbutton1"> <property name="label" translatable="yes">checkbutton</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">False</property> <property name="draw_indicator">True</property> <signal name="clicked" handler="on_checkbutton1_clicked"/> </widget> <packing> <property name="position">0</property> </packing> </child> <child> <widget class="GtkCheckButton" id="checkbutton2"> <property name="label" translatable="yes">checkbutton</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">False</property> <property name="draw_indicator">True</property> <signal name="clicked" handler="on_checkbutton2_clicked"/> </widget> <packing> <property name="position">1</property> </packing> </child> <child> <widget class="GtkEntry" id="entry1"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </widget> <packing> <property name="position">2</property> </packing> </child> </widget> </child> </widget> </glade-interface>
打开Python Shell
菜单-File-New Windows
输入以下代码 (代码解释)
import os
import sys
import pygtk
import gtk
import gtk.glade
class gtkCheckBox:
Xml = None
Checkbox1 = None
Checkbox2 = None
Text1 = None
def __init__(self):
self.Xml = gtk.glade.XML("gtkCheckBox.glade")
self.window=self.Xml.get_widget("MainWindow")
self.Checkbox1=self.Xml.get_widget("checkbutton1")
self.Checkbox2=self.Xml.get_widget("checkbutton2")
self.Text1=self.Xml.get_widget("entry1")
self.Xml.signal_connect("on_MainWindow_destroy", self.destroy)
self.Xml.signal_connect("on_checkbutton1_clicked", self.Checkbutton1_Clicked)
self.Xml.signal_connect("on_checkbutton2_clicked", self.Checkbutton2_Clicked)
self.window.show()
gtk.main()
def destroy(self,widget,data=None):
gtk.main_quit()
def Checkbutton1_Clicked(self, widget):
self.Text1.set_text("CheckBox 1 %s" % self.Checkbox1.get_active())
def Checkbutton2_Clicked(self, widget):
self.Text1.set_text("CheckBox 2 %s" % self.Checkbox2.get_active())
_gtk = gtkCheckBox()
import os import sys import pygtk import gtk import gtk.glade class gtkCheckBox: Xml = None Checkbox1 = None Checkbox2 = None Text1 = None def __init__(self): self.Xml = gtk.glade.XML("gtkCheckBox.glade") self.window=self.Xml.get_widget("MainWindow") self.Checkbox1=self.Xml.get_widget("checkbutton1") self.Checkbox2=self.Xml.get_widget("checkbutton2") self.Text1=self.Xml.get_widget("entry1") self.Xml.signal_connect("on_MainWindow_destroy", self.destroy) self.Xml.signal_connect("on_checkbutton1_clicked", self.Checkbutton1_Clicked) self.Xml.signal_connect("on_checkbutton2_clicked", self.Checkbutton2_Clicked) self.window.show() gtk.main() def destroy(self,widget,data=None): gtk.main_quit() def Checkbutton1_Clicked(self, widget): self.Text1.set_text("CheckBox 1 %s" % self.Checkbox1.get_active()) def Checkbutton2_Clicked(self, widget): self.Text1.set_text("CheckBox 2 %s" % self.Checkbox2.get_active()) _gtk = gtkCheckBox()