简体   繁体   English

在python3 + gtk3中获取文本

[英]get text in python3+gtk3

This is possibly more a python3 question the a gtk3 . 这可能更多是python3问题和gtk3问题。 In this following bit of code, print(numele) is working correctly, ie the connect function self.nument.connect("activate",self.get_nument) is ok. 在下面的代码中,print(numele)正常工作,即connect函数self.nument.connect("activate",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()

But, in the for loop , it can not access the numele value. 但是,在for loop ,它无法访问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

I am new to python. 我是python的新手。 So, I am expecting numele is private to the function get_nument. 因此,我希望numele对于get_nument函数是私有的。 What is the best way to use it outside the function(ie public)? 在功能(即公共)之外使用它的最佳方法是什么?

Kindly Help. 请帮助。

EDIT: after ptomato's reply I tried exactly that, with: 编辑:在ptomato的回复之后,我尝试了以下操作:

#Connect Entry nument
    self.nument.connect("activate",self.get_nument)
    print(self.numele)

and defining the function as: 并将函数定义为:

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

Only to have error: 只是有错误:

$ 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'

You have exactly the right idea. 您的想法完全正确。 The numele variable is locally scoped, so it gets deleted at the end of the function. numele变量是局部作用域的,因此在函数末尾将其删除。 Either return the value from your function, or store it self.numele instead. 从函数中return值,或者将其存储为self.numele That stores it in your EntryWindow object so that it persists beyond the scope of the function. 它将其存储在EntryWindow对象中,以便其持久存在,超出函数的范围。

PS. PS。 You might need to do int(numele) before you can use it in the range() call... 您可能需要先进行int(numele)才能在range()调用中使用它...

The code has several mistakes and I am not sure what you want to achieve. 该代码有几个错误,我不确定您要实现什么。 You probably want to have some Gtk.Entries based on the number from the first entry. 您可能想根据第一个条目中的数字创建一些Gtk.Entries。 Therefore, you have to move the for-loop into the get_nument method. 因此,您必须将for循环移到get_nument方法中。 This will still throw an error because get_nument is too much indented. 这仍然会引发错误,因为get_nument缩进过多。 Moreover, you need to convert the string from the Gtk.Entry to an integer. 此外,您需要将字符串从Gtk.Entry转换为整数。 Finally, the grid has to be an attribute to be accessible for get_nument . 最后,网格必须是get_nument可以访问的属性。

The result should look like this: 结果应如下所示:

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()

This code still has problems, for example if a lower number is entered, the entries are not removed from the grid. 此代码仍然有问题,例如,如果输入的数字较小,则不会从网格中删除条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM