Python Gtk name Error

I have created a Gtk+3 app to add two numbers and wanted to put any functions used into a separate file. The app is working as a single file but when split into two files I am getting an error - NameError: name ‘entry1’ is not defined

addition.py -

import gi
gi.require_version(‘Gtk’, ‘3.0’)
from gi.repository import Gtk

import functions

class MyWindow(Gtk.Window):
def init(self):
Gtk.Window.init(self, title=“Add Two Numbers”)
self.set_default_size(300, 200)

    self.box = Gtk.Box(orientation="vertical", spacing=2)
    self.add(self.box)

    self.label = Gtk.Label.new_with_mnemonic("") 
    self.label_num1 = Gtk.Label.new_with_mnemonic("Enter number 1: ")
    self.label_num2 = Gtk.Label.new_with_mnemonic("Enter number 2: ")

    self.entry1 = Gtk.Entry()        
    self.entry2 = Gtk.Entry()

    self.btn1 = Gtk.Button(label="Add")
    self.btn1.connect("clicked", functions.btn_clicked)

    self.box.pack_start(self.label, True, True, 0)
    self.box.pack_start(self.label_num1, True, True, 0)
    self.box.pack_start(self.entry1, True, True, 0)
    self.box.pack_start(self.label_num2, True, True, 0)
    self.box.pack_start(self.entry2, True, True, 0)
    self.box.pack_start(self.btn1, True, True, 0)

win = MyWindow()
win.connect(“destroy”, Gtk.main_quit)
win.show_all()
Gtk.main()

functions.py-

import gi
gi.require_version(‘Gtk’, ‘3.0’)
from gi.repository import Gtk

def btn_clicked(widget):
print(“clicked”)
num1 = (int(entry1.get_text()))
num2 = (int(self.entry2.get_text()))
sum = (num1 + num2)
self.label.set_text("Result = " + (str(sum)))

Any help would be much appreciated.

This is an issue with your understanding of Python, not GTK. Names (being variables, functions, classes, …) are only available in the same file, unless imported and referenced in another file.

functions.py has no idea about the names in additions.py. Not only entry1, but also self doesn’t exist in there.

It also doesn’t make sense, from a readibility standpoint, to move that callback function to another file. You can pass all required names to that function, but at this point it’s just too tightly coupled to the MyWindow class to do this.

Just to be clear, here’s a stupid basi Python example that shows the issue.

a.py

import b

print("Hello from a.py")
foo = "test"
b.bar()

b.py

print("Hello from b.py")

def bar():
    print(foo)  # This will raise a NameError

I advise you to get back to the Python learning material on modules to get a better understanding.

Hi @Paul_Divers ,

Usually it’s not a good idea to split functions like this. Try to keep code that interact with your window’s widgets altogether in the same class, that will ease up the code maintenance, and you will have cleaner abstractions.

If you really want to split, then try something like this:

  1. pass your MyWindow instance self as callback parameter:
self.btn1.connect("clicked", functions.btn_clicked, self)
  1. get the window as param in the callback function:
def btn_clicked(widget, my_window):
    print(“clicked”)
    # TODO: add try/except block, entry1 and entry2 may not contain integers
    num1 = (int(my_window.entry1.get_text()))
    num2 = (int(my_window.entry2.get_text()))

Thanks for your replies folks, it’s working now and I appreciate the advice about gaining a better understanding of Python modules. My goal here was to learn how data and signals can be sent and received from files outside the GUI. The code is now:

Functions.py

import gi
gi.require_version(‘Gtk’, ‘3.0’)
from gi.repository import Gtk

def btn_clicked(widget, win):
print(“clicked”)
num1 = (int(win.entry1.get_text()))
num2 = (int(win.entry2.get_text()))
sum = (num1 + num2)
win.label.set_text("Result = " + (str(sum)))

Addition.py

import gi
gi.require_version(‘Gtk’, ‘3.0’)
from gi.repository import Gtk

import functions

class MyWindow(Gtk.Window):
def init(self):
Gtk.Window.init(self, title=“Add Two Numbers”)
self.set_default_size(300, 200)

    self.box = Gtk.Box(orientation="vertical", spacing=2)
    self.add(self.box)

    self.label = Gtk.Label.new_with_mnemonic("") 
    self.label_num1 = Gtk.Label.new_with_mnemonic("Enter number 1: ")
    self.label_num2 = Gtk.Label.new_with_mnemonic("Enter number 2: ")

    self.entry1 = Gtk.Entry()        
    self.entry2 = Gtk.Entry()

    self.btn1 = Gtk.Button(label="Add")
    self.btn1.connect("clicked", functions.btn_clicked, self)

    self.box.pack_start(self.label, True, True, 0)
    self.box.pack_start(self.label_num1, True, True, 0)
    self.box.pack_start(self.entry1, True, True, 0)
    self.box.pack_start(self.label_num2, True, True, 0)
    self.box.pack_start(self.entry2, True, True, 0)
    self.box.pack_start(self.btn1, True, True, 0)

win = MyWindow()
win.connect(“destroy”, Gtk.main_quit)
win.show_all()
Gtk.main()

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.