PyGTK 计算器

问题描述 投票:0回答:1

我想用Python GTK制作一个计算器。我正在尝试获取一个按钮来在应用程序中显示我的文本。但是,我的函数给了我这个错误

TypeError: Calculator.clickedButton1() takes 1 positional argument but 2 were given
。我了解 self 和参数,但我尝试了一个变量并将
Gtk.Entry
传递到函数中,但它不起作用。

stackoverflow 上只有一篇类似的帖子但从未解决。 如何在 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()
python-3.x pygtk
1个回答
0
投票

答案是将 self 传递到函数中。显然,自我并不像我想象的那样隐含。感谢@mkrieger1。

    def clickedButton1(self, entry):
        text = self.entry.get_text()
        text += "1"
        self.entry.set_text(text)
© www.soinside.com 2019 - 2024. All rights reserved.