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.