I want to make a calculator with Python GTK. I am trying to get a button to display my text in the application. However, my function gives me this error TypeError: Calculator.clickedButton1() takes 1 positional argument but 2 were given
. I know about self and arguments, but I have attempted a variable and passing Gtk.Entry
into the function, but it doesn't work.
There was only one stackoverflow post that was similar but never solved. How do i do calculations in Python Gtk?
# this is what the application looks like simply.
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class Calculator(Gtk.Window): # my sub class
def __init__(self):
super().__init__(title="Calculator") # the base class Gtk.Window
self.set_border_width(10)
# the header and title bar
headerBar = Gtk.HeaderBar()
headerBar.set_show_close_button(True)
headerBar.props.title = "Calculator"
self.set_titlebar(headerBar)
# this is my button and calling the function.
self.button1 = Gtk.Button(label="1")
self.button1.connect("clicked", self.clickedButton1)
# creating the entry field
self.entry = Gtk.Entry()
self.entry.set_text("")
# this creates the grid
grid = Gtk.Grid()
grid.add(self.button1)
grid.attach_next_to(self.entry,self.button1,Gtk.PositionType.TOP,4,4)
self.add(grid)
# my function
def clickedButton1():
self.entry.set_text("1")
return self.entry
# creating the actual window and running continuosly.
win = Calculator()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
The answer is to pass self into the function. Apparently, the self is not implied like I believed. Credit goes to @mkrieger1.
def clickedButton1(self, entry):
text = self.entry.get_text()
text += "1"
self.entry.set_text(text)