[英]Why don't my form controls (Glade/Python) work?
我开发了一个简单的林间空地形式,我打算用 python-3.10 进行操作。
我以为我的代码是正确的,但控件就是没有响应。 (特别是按钮)。
如果有人可以提供帮助,我将不胜感激。 我认为主要 Gui class 中的“connect_symbols(self)”会处理它,但我仍然缺少一些东西。
这是程序:
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import sys
import os
builder = Gtk.Builder()
class GUI:
def __init__ (self):
builder.add_from_file('Form-10a.glade')
builder.connect_signals(self)
self.Pindir = None
self.Poutdir = None
self.Psortby = 'type'
self.Preport = 'False'
self.Pverbose = 'True'
window = builder.get_object('window1')
window.show_all()
#
def onSubmitButtonCli#cked(self, button):
inputdir = builder.get_object('inputdir').get_property('text')
print(f'inputdir = {inputdir}')
tv = os.path.exists(inputdir)
if (tv == False):
print(inputdir, "is not a vaild directory, try again.")
else:
self.Pindir = inputdir
outputdir = builder.get_object('outputdir').get_property('text')
print(f'outputdir = {outputdir}')
self.Poutdir=outputdir
name_button = builder.get_object('NameButton')
#print('[NameButton] active:', name_button.get_active())
#print('[NameButton] group :', name_button.get_group())
for item in name_button.get_group():
name = item.get_name()
truth_value = item.get_active()
print(name, '=', truth_value)
if (truth_value == True):
self.Psortby = name
i = self.Psortby.index('Button')
self.Psortby=self.Psortby[0:i]
report_button = builder.get_object('ReportY')
for item in report_button.get_group():
name = item.get_name()
truth_value = item.get_active()
print(name, '=' , truth_value)
self.Preport = truth_value
verbose_button = builder.get_object('VerboseY')
for item in verbose_button.get_group():
name = item.get_name()
truth_value = item.get_active()
print(name, '=', truth_value)
# tv = ((truth_value=='True')?1:0)
self.Pverbose = truth_value
with open('scripts-search.conf','w') as w:
w.write('[Main]\n')
outputdata = 'inputdir: ' + self.Pindir + '\n'
w.write(outputdata)
outputdata = 'outputdir: ' + self.Poutdir + '\n'
w.write(outputdata)
outputdata = 'sortby: ' + str(self.Psortby) + '\n'
w.write(outputdata)
outputdata = 'report: ' +str(self.Preport) + '\n'
w.write(outputdata)
outputdata = 'verbose: ' + str(self.Pverbose) + '\n'
w.write(outputdata)
w.write('\n')
Gtk.main_quit()
def onDestroy(self,menuitem):
Gtk.main_quit()
def onQuitButtonClicked(self,button):
Gtk.main_quit()
def main():
while True:
try:
app = GUI()
Gtk.main()
except Exception as e:
print ("Error!!")
print (e.inst)
Gtk.main_quit()
if __name__ == '__main__':
rv = 0
rv = main()
sys.exit(rv)
而且,如果有人感兴趣,这里是 Form-10a.glade:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window1">
<property name="name">GtkWindow</property>
<property name="can-focus">True</property>
<property name="window-position">center-always</property>
<property name="default-width">380</property>
<property name="default-height">200</property>
<property name="destroy-with-parent">True</property>
<property name="type-hint">dialog</property>
<property name="gravity">north-east</property>
<signal name="destroy" handler="onDestroy" swapped="no"/>
<child>
<!-- n-columns=2 n-rows=7 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
<object class="GtkLabel">
<property name="name">label1</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-left">3</property>
<property name="margin-right">3</property>
<property name="margin-start">3</property>
<property name="margin-end">3</property>
<property name="margin-top">3</property>
<property name="margin-bottom">3</property>
<property name="label" translatable="yes">Scripts Search - Enter Parameters</property>
<property name="justify">fill</property>
<attributes>
<attribute name="font-desc" value="Serif 12"/>
<attribute name="style" value="normal"/>
<attribute name="weight" value="normal"/>
<attribute name="variant" value="normal"/>
<attribute name="stretch" value="normal"/>
<attribute name="scale" value="1"/>
</attributes>
<signal name="destroy" handler="onDestroy" swapped="no"/>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="GtkWindow">
<property name="name">GtkWindow</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="label" translatable="yes">
</property>
<property name="justify">center</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="label" translatable="yes">Directory to search:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="label" translatable="yes">Direcotry for output:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="label" translatable="yes">Sort by:</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="label" translatable="yes">Printed Report? (y/n):</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="label" translatable="yes">Verbose? (y/n):</property>
<property name="justify">right</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="inputdir">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="text" translatable="yes">/usr/bin</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="outputdir">
<property name="name">outputdir</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="text">/home/baz/misc/docs</property>
<signal name="destroy" handler="onDestroy" swapped="no"/>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkButtonBox" id="inputdi">
<property name="name">inputdir</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="layout-style">start</property>
<signal name="destroy" handler="onDestroy" swapped="no"/>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
<!-- n-columns=3 n-rows=1 -->
<object class="GtkGrid" id="radiobutton">
<property name="name">radiobutton</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
<object class="GtkRadioButton" id="NameButton">
<property name="label" translatable="yes">Name </property>
<property name="name">NameButton</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="SizeButton">
<property name="label" translatable="yes">Size </property>
<property name="name">SizeButton</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="draw-indicator">True</property>
<property name="group">NameButton</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="TypeButton">
<property name="label" translatable="yes">Type </property>
<property name="name">TypeButton</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="draw-indicator">True</property>
<property name="group">NameButton</property>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">3</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=1 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkRadioButton" id="ReportY">
<property name="label" translatable="yes">Y </property>
<property name="name">ReportY</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="ReportN">
<property name="label" translatable="yes">N </property>
<property name="name">ReportN</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
<property name="group">ReportY</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=1 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkRadioButton" id="VerboseY">
<property name="label" translatable="yes">Y </property>
<property name="name">VerboseY</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="VerboseN">
<property name="label" translatable="yes"></property>
<property name="name">VerboseN</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">False</property>
<property name="active">True</property>
<property name="draw-indicator">True</property>
<property name="group">VerboseY</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=1 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkButton" id="SaveButton">
<property name="label" translatable="yes">Save</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="QuitButton">
<property name="label" translatable="yes">Quit WIthout Saving</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="onQuitButtonClicked" swapped="no"/>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
<object class="GtkButton" id="SubmitButtton">
<property name="label" translatable="yes">Submit</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="onSubmitButtonClicked" swapped="no"/>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">6</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
我对 Python 有点生疏,但是当我尝试你的代码和空地文件时,我得到了不同的行为。 每当我单击按钮或尝试关闭应用程序时,新的 windows 就会不断产生。 我不得不手动终止应用程序。 我为使事情正常进行所做的小修改是通过删除“while True:”测试来简化主块。
def main():
try:
app = GUI()
Gtk.main()
except Exception as e:
print ("Error!!")
print (e.inst)
Gtk.main_quit()
之后,“提交”按钮产生 output,“不保存退出”按钮起作用。 我在你的空地文件中注意到你还没有为“保存”按钮设置信号。
希望有所帮助。
问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.