[英]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.