Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python3+;gtk3_Python_Python 3.x_Gtk_Gtk3 - Fatal编程技术网

在python3+;gtk3

在python3+;gtk3,python,python-3.x,gtk,gtk3,Python,Python 3.x,Gtk,Gtk3,这可能更像是一个关于agtk3的问题。 在以下代码位中,打印(numele)工作正常,即connect功能self.nument.connect(“激活”,self.get\nument)正常 from gi.repository import Gtk, GObject class EntryWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Entry Demo") self.

这可能更像是一个关于a
gtk3
的问题。 在以下代码位中,打印(numele)工作正常,即
connect
功能
self.nument.connect(“激活”,self.get\nument)
正常

from gi.repository import Gtk, GObject

class EntryWindow(Gtk.Window):

  def __init__(self):
    Gtk.Window.__init__(self, title="Entry Demo")
    self.set_size_request(100, 50)

    grid=Gtk.Grid()
    self.add(grid)

#Create Entry nument
    self.nument = Gtk.Entry()
    self.numlab = Gtk.Label()
    self.numlab.set_text("Number Of Element")
    self.nument.set_text("Number Of Element")
    self.nument.set_editable("TRUE")
    grid.attach(self.numlab, 0,2,1,1)      
    grid.attach(self.nument, 1,2,1,1)      
#Connect Entry nument
    self.nument.connect("activate",self.get_nument)

#Create Entry from numele
    for i in range(1,numele+1):
      self.entry = Gtk.Entry()
      self.entry.set_text("Hello World")
      self.entry.set_editable("FALSE")
      grid.attach(self.entry, 0,2+i,1,1)


    def get_nument(self,entry):
      numele= self.nument.get_text()
      print(numele)

win = EntryWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
但是,在循环的
中,它无法访问numele值

 $python3 hw3.py 
Traceback (most recent call last):
  File "hw3.py", line 35, in <module>
    win = EntryWindow()
  File "hw3.py", line 24, in __init__
    for i in range(1,numele+1):
NameError: global name 'numele' is not defined
并将该功能定义为:

def获取设备(自我,输入): self.numele=self.nument.get_text()

只有出现错误时:

$ python3 test.py
Traceback (most recent call last):
  File "test.py", line 35, in <module>
    win = EntryWindow()
  File "test.py", line 21, in __init__
    self.nument.connect("activate",self.get_nument)
AttributeError: 'EntryWindow' object has no attribute 'get_nument'
$python3 test.py
回溯(最近一次呼叫最后一次):
文件“test.py”,第35行,在
win=EntryWindow()
文件“test.py”,第21行,在_init中__
self.nument.connect(“激活”,self.get_nument)
AttributeError:“EntryWindow”对象没有属性“get\u nument”

你的想法完全正确。
numele
变量的作用域是局部的,因此它会在函数末尾被删除。
返回函数中的值,或者将其存储为
self.numele
。它将它存储在
EntryWindow
对象中,以便它在函数范围之外继续存在


另外,您可能需要先执行
int(numele)
,然后才能在
range()调用中使用它。

代码有几个错误,我不确定您想要实现什么。 您可能希望根据第一个条目的编号创建一些Gtk.Entries。因此,必须将for循环移动到
get\u nument
方法中。这仍然会引发错误,因为
get\u nument
缩进太多。此外,还需要将Gtk.Entry中的字符串转换为整数。 最后,网格必须是可供
get\u nument
访问的属性

结果应该如下所示:

from gi.repository import Gtk, GObject

class EntryWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Entry Demo")
        self.set_size_request(100, 50)

        self.grid=Gtk.Grid()
        self.add(self.grid)

        #Create Entry nument
        self.nument = Gtk.Entry()
        self.numlab = Gtk.Label()
        self.numlab.set_text("Number Of Element")
        self.nument.set_editable("TRUE")
        self.grid.attach(self.numlab, 0,2,1,1)
        self.grid.attach(self.nument, 1,2,1,1)
        #Connect Entry nument
        self.nument.connect("activate",self.get_nument)



    def get_nument(self,entry):
        numele= self.nument.get_text()
        print(numele)
        #Create Entry from numele
        for i in range(1,int(numele)+1):
            entry = Gtk.Entry()
            entry.set_text("Hello World")
            entry.set_editable("FALSE")
            entry.show()
            self.grid.attach(entry, 0,2+i,1,1)

win = EntryWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
此代码仍然存在问题,例如,如果输入的数字较低,则不会从网格中删除条目

from gi.repository import Gtk, GObject

class EntryWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Entry Demo")
        self.set_size_request(100, 50)

        self.grid=Gtk.Grid()
        self.add(self.grid)

        #Create Entry nument
        self.nument = Gtk.Entry()
        self.numlab = Gtk.Label()
        self.numlab.set_text("Number Of Element")
        self.nument.set_editable("TRUE")
        self.grid.attach(self.numlab, 0,2,1,1)
        self.grid.attach(self.nument, 1,2,1,1)
        #Connect Entry nument
        self.nument.connect("activate",self.get_nument)



    def get_nument(self,entry):
        numele= self.nument.get_text()
        print(numele)
        #Create Entry from numele
        for i in range(1,int(numele)+1):
            entry = Gtk.Entry()
            entry.set_text("Hello World")
            entry.set_editable("FALSE")
            entry.show()
            self.grid.attach(entry, 0,2+i,1,1)

win = EntryWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()