This is possibly more a python3
question the a 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.
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.
$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. So, I am expecting numele is private to the function 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:
#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()
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. Either return
the value from your function, or store it self.numele
instead. That stores it in your EntryWindow
object so that it persists beyond the scope of the function.
PS. You might need to do int(numele)
before you can use it in the range()
call...
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. Therefore, you have to move the for-loop into the get_nument
method. This will still throw an error because get_nument
is too much indented. Moreover, you need to convert the string from the Gtk.Entry to an integer. Finally, the grid has to be an attribute to be accessible for 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.