[英]Python/Glade/PyGTK - Check if Checkbox is Active
After going through the PyGTK libraries and tutorials, I haven't managed to find an exact answer I'm looking for, or maybe I'm just doing something wrong... 在浏览了PyGTK库和教程之后,我还没有找到确切的答案,或者我只是做错了什么...
Anyway, I'm learning PyGTK and Glade, I'm in the process of scripting a simple downloader application. 无论如何,我正在学习PyGTK和Glade,我正在编写一个简单的下载器应用程序的脚本。 The GUI of the application has three check boxes, and then a submit button. 该应用程序的GUI具有三个复选框,然后是一个提交按钮。
What I'm trying to do is get the status of the check boxes after the submit button is pressed, that way I can continue my script to download applications based on whether the checkboxes are true or false. 我想做的是在按下“提交”按钮后获取复选框的状态,这样我就可以根据复选框是true还是false继续执行脚本以下载应用程序。
Here's what I have so far. 到目前为止,这就是我所拥有的。 (Still very early stages and learning from mistakes) (仍处于早期阶段并从错误中学习)
Python Script: 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): 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>
Either I can't find the proper method of calling for the status, or I have found it and I'm just using it wrong... Any help is greatly appreciated, thanks in advance! 我找不到合适的状态调用方法,或者我找到了它,而我只是在错误地使用它...非常感谢您的帮助,在此先感谢您!
you just need to call "get_active" method of gtk checkbox, get object of check button from glade file and call "get_active" method on click event of submit button. 您只需要调用gtk复选框的“ get_active”方法,从林间空地文件获取检查按钮的对象,并在提交按钮的click事件上调用“ get_active”方法。
here, code snippet to be more descriptive: 在这里,代码段更具描述性:
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()
hope, it would help you. 希望对您有帮助。
In this callback: 在此回调中:
def btnSubmit_clicked(self, widget):
self.button = gtk.ToggleButton("chkboxDropbox_toggled")
status = self.button.get_active()
print status
what you are actually doing is creating a completely new toggle button, which is invisible because you're not adding it to any other widget, and then asking it for its status. 您实际上正在做的是创建一个全新的切换按钮,该按钮不可见,因为您没有将其添加到任何其他小部件中,然后询问其状态。 That's why it's not responding as you expect. 这就是为什么它没有像您期望的那样响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.