gpt4 book ai didi

Python/Glade/PyGTK - 检查复选框是否处于事件状态

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:59 25 4
gpt4 key购买 nike

在浏览了 PyGTK 库和教程之后,我还没有设法找到我正在寻找的确切答案,或者也许我只是做错了什么......

无论如何,我正在学习 PyGTK 和 Glade,我正在编写一个简单的下载器应用程序的脚本。应用程序的 GUI 有三个复选框,然后是一个提交按钮。

我想做的是在按下提交按钮后获取复选框的状态,这样我就可以根据复选框是真还是假继续我的脚本来下载应用程序。

这是我目前所拥有的。 (仍处于早期阶段并从错误中吸取教训)

Python 脚本:

import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)


class GladeTest:

def __init__(self):

#Set the Glade file
filename = "gui.glade"
builder = gtk.Builder()
builder.add_from_file(filename)
builder.connect_signals(self)

#Create our dictionay and connect it
dic = { "btnSubmit_clicked" : self.btnSubmit_clicked,
"chkboxDropbox_toggled" : self.chkboxDropbox_toggled,
"MainWindow_destroy" : gtk.main_quit }

def btnSubmit_clicked(self, widget):
self.button = gtk.ToggleButton("chkboxDropbox_toggled")
status = self.button.get_active()
print status

def chkboxDropbox_toggled(self, widget):
print ""




if __name__ == "__main__":
hwg = GladeTest()
gtk.main()

Glade 3 XML(gui.glade):

<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkAction" id="action1"/>
<object class="GtkWindow" id="MainWindow">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">MainWindow</property>
<property name="resizable">False</property>
<property name="window_position">center</property>
<signal name="destroy" handler="MainWindow_destroy" swapped="no"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Downloader</property>
<attributes>
<attribute name="style" value="normal"/>
<attribute name="size" value="300"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxDropbox">
<property name="label" translatable="yes">Dropbox</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="chkboxDropbox_toggled" swapped="no"/>
<signal name="activate" handler="chkboxDropbox_active" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxPython">
<property name="label" translatable="yes">Python</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="chkboxPython_checked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="chkboxChrome">
<property name="label" translatable="yes">Google Chrome</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_action_appearance">False</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="chkboxChrome_checked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnSubmit">
<property name="label" translatable="yes">Download/Run</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="btnSubmit_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

要么我找不到调用状态的正确方法,要么我找到了但我只是用错了...非常感谢任何帮助,提前致谢!

最佳答案

您只需要调用 gtk 复选框的“get_active”方法,从 glade 文件中获取复选框对象,并在提交按钮的点击事件上调用“get_active”方法。

此处,代码片段更具描述性:

class GladeTest:

def __init__(self):

#Set the Glade file
filename = "gui.glade"
self.builder = gtk.Builder()
self.builder.add_from_file(filename)
self.builder.connect_signals(self)

#Create our dictionay and connect it
dic = { "btnSubmit_clicked" : self.btnSubmit_clicked,
"chkboxDropbox_toggled" : self.chkboxDropbox_toggled,
"MainWindow_destroy" : gtk.main_quit }

def btnSubmit_clicked(self, widget):
chkbt_chrome = self.builder.get_object("chkboxChrome")
print "ACTIVE--",chkbt_chrome.get_active()
# do same for rest of the check buttons

self.button = gtk.ToggleButton("chkboxDropbox_toggled") # if used for check box status then there is no need for this button.

status = self.button.get_active()
print status

def chkboxDropbox_toggled(self, widget):
print ""


if __name__ == "__main__":
hwg = GladeTest()
gtk.main()

希望对你有帮助。

关于Python/Glade/PyGTK - 检查复选框是否处于事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com