首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的表单控件(Glade/Python)不能工作?

为什么我的表单控件(Glade/Python)不能工作?
EN

Stack Overflow用户
提问于 2022-05-29 17:49:22
回答 1查看 20关注 0票数 0

我开发了一个简单的glade表单,我想用python-3.10来操纵它。

我以为我的代码是正确的,但是控件就是没有响应。(特别是按钮)。

如果有人能帮忙,我会非常感激的。我以为主Gui类中的"connect_symbols(self)“会处理好它,但我仍然遗漏了一些东西。

以下是节目:

代码语言:javascript
复制
#!/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)

如果有人感兴趣的话,这里是表格-10a.glade:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2022-05-31 01:17:06

我对Python有点生疏,但是当我尝试您的代码和glade文件时,我得到了不同的行为。每当我点击按钮或试图关闭应用程序时,新的窗口就会不断涌现。我不得不手动关闭应用程序。为了使事情正常工作,我做了一个小的修订,就是通过删除"while True:“测试来简化主块。

代码语言:javascript
复制
def main():
    try:
        app = GUI()
        Gtk.main()
    except Exception as e:
        print ("Error!!")
        print (e.inst)
        Gtk.main_quit()

之后,"Submit“按钮产生输出,”退出而不保存“按钮工作。我在你的glade文件中注意到,你还没有为“保存”按钮设置任何信号。

希望这能有所帮助。

致以问候。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72425988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档