快速切换文档

快速切换文档

有人知道我可以在哪里找到有关 glade 上的 Switch Widget 的文档吗?

我尝试在 GTK 网站上查找,但没有它的条目。

编辑

感谢 Timo 为我指明了正确的方向。我很喜欢 askubuntu!

基本上我的问题是,默认情况下,连接函数会将窗口的对象、小部件的对象和活动状态推送到您在回调上运行的任何函数。

这是之前的样子:

self.daymon.connect("notify::active", self.toggle_day("mon"))

之后的情况如下:

self.daymon.connect('notify::active', self.toggle_day, "mon")

运行以下函数时,后者效果很好:

def toggle_day(self, widget, active, day):  
    if widget.get_active():  
        state = True  
    else:  
        state = False  

如果您感兴趣的话可以查看完整代码:

# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE

import gettext
from gettext import gettext as _
gettext.textdomain('snooze')

from gi.repository import Gtk # pylint: disable=E0611
import logging
logger = logging.getLogger('snooze')

import ConfigParser, os # We need to be able to store and recal settings

from snooze_lib import Window
from snooze.AboutSnoozeDialog import AboutSnoozeDialog
from snooze.PreferencesSnoozeDialog import PreferencesSnoozeDialog

#first try to read the config.cfg file
config = ConfigParser.RawConfigParser()
configFile = 'data/config.cfg'
monState = False
tueState = False
wedState = False
thurState = False
friState = False
satState = False
sunState = False

# Creating the Config file
def createConfigFile(fileName):
    print "CREATING CONFIG"
    config.add_section('Preferences')
    config.set('Preferences', 'mon', False)
    config.set('Preferences', 'tues', False)
    config.set('Preferences', 'wed', False)
    config.set('Preferences', 'thur', False)
    config.set('Preferences', 'fri', False)
    config.set('Preferences', 'sat', False)
    config.set('Preferences', 'sun', False)
    rewriteConfigFile(filename)

# Writing our configuration file to the failename as specifeid
def rewriteConfigFile(filename):    
    with open(filename, 'wb') as configfile:
        config.write(configfile)

# Reading the config file 
def readConfigFile(fileName):
    print "READING CONFIG"
    global monState, tueState, wedState, thurState, friState, satState, sunState
    monState = config.getboolean('Preferences', 'mon')
    tueState = config.getboolean('Preferences', 'tues')
    wedState = config.getboolean('Preferences', 'wed')
    thurState = config.getboolean('Preferences', 'thur')
    friState = config.getboolean('Preferences', 'fri')
    satState = config.getboolean('Preferences', 'sat')
    sunState = config.getboolean('Preferences', 'sun')

# If the config does not exist, create it then read it. Otherwise read it
if not config.read(configFile):    
    createConfigFile(configFile)
    readConfigFile(configFile)    
else:
    readConfigFile(configFile)

# See snooze_lib.Window.py for more details about how this class works
class SnoozeWindow(Window):
    __gtype_name__ = "SnoozeWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(SnoozeWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutSnoozeDialog
        self.PreferencesDialog = PreferencesSnoozeDialog

        # Code for other initialization actions should be added here.
        self.daymon = self.builder.get_object("daymon")
        self.daytues = self.builder.get_object("daytues")
        self.daywed = self.builder.get_object("daywed")
        self.daythur = self.builder.get_object("daythur")
        self.dayfri = self.builder.get_object("dayfri")
        self.daysat = self.builder.get_object("daysat")
        self.daysun = self.builder.get_object("daysun")

        # Set the values based on the config file        
        if monState == True:
            self.daymon.activate()

        if tueState == True:
            self.daytues.activate()

        if wedState == True:
            self.daywed.activate()

        if thurState == True:
            self.daythur.activate()

        if friState == True:
            self.dayfri.activate()

        if satState == True:
            self.daysat.activate()

        if sunState == True:
           self.daysun.activate()

        self.daymon.connect('notify::active', self.toggle_day, "mon")
        self.daytues.connect('notify::active', self.toggle_day, "tues")
        self.daywed.connect('notify::active', self.toggle_day, "wed")
        self.daythur.connect('notify::active', self.toggle_day, "thur")
        self.dayfri.connect('notify::active', self.toggle_day, "fri")
        self.daysat.connect('notify::active', self.toggle_day, "sat")
        self.daysun.connect('notify::active', self.toggle_day, "sun")

    # Toggle the setting and store the information in the config file
    def toggle_day(self, widget, active, day):
        if widget.get_active():
            state = True
        else:
            state = False

        # Set the config option and update the config file
        global configFile
        config.set('Preferences', day, state)
        rewriteConfigFile(configFile)

答案1

干得好:http://developer.gnome.org/gtk3/stable/GtkSwitch.html

包含所有小部件和更多有用信息的完整参考:http://developer.gnome.org/gtk3/stable/

GtkSwitch 只有一个信号,activate。根据文档,您不应该直接连接到它:

应用程序永远不应该连接到该信号,而应该使用notify::active信号。

因此连接应该很简单:

switch.connect('notify::active', _switch_active_changed_cb)

notify::foo原理可用于任何小部件,它实际上监听属性的变化。

阅读有关连接信号的更多信息文档

相关内容