pygtk-check buttons

本文介绍了使用Python语言实现GUI应用程序的基本步骤,并通过一个简单的例子展示了如何创建具有交互功能的界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

liuxing@liuxing-O-E-M:~/pro/pytest$ python test1.py

check button 1 was toggled ON

check button 1 was toggled OFF

check button 2 was toggled ON

check button 1 was toggled ON

check button 1 was toggled OFF





#!/usr/bin/env python
# example checkbutton.py

import pygtk
pygtk.require('2.0')
import gtk

class CheckButton:
# Our callback.
# The data passed to this method is printed to stdout
    def callback(self, widget, data=None):
        print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
 # This callback quits the program
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False
    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        # Set the window title
        self.window.set_title("Check Button")
        # Set a handler for delete_event that immediately
        # exits GTK.
        self.window.connect("delete_event", self.delete_event)
        # Sets the border width of the window.
        self.window.set_border_width(20)
        # Create a vertical box
        vbox = gtk.VBox(True, 2)
        # Put the vbox in the main window
        self.window.add(vbox)
        # Create first button
        button = gtk.CheckButton("check button 1")
        # When the button is toggled, we call the "callback" method
        # with a pointer to "button" as its argument
        button.connect("toggled", self.callback, "check button 1")
        # Insert button 1
        vbox.pack_start(button, True, True, 2)
        button.show()
        # Create second button
        button = gtk.CheckButton("check button 2")
        # When the button is toggled, we call the "callback" method
        # with a pointer to "button 2" as its argument
        button.connect("toggled", self.callback, "check button 2")
        # Insert button 2
        vbox.pack_start(button, True, True, 2)
        button.show()
        # Create "Quit" button
        button = gtk.Button("Quit")
        # When the button is clicked, we call the mainquit function
        # and the program exits
        button.connect("clicked", lambda wid: gtk.main_quit())
        # Insert the quit button
        vbox.pack_start(button, True, True, 2)
        button.show()
        vbox.show()
        self.window.show()
def main():
    gtk.main()
    return 0
if __name__ == "__main__":
    CheckButton()
    main()
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值