提问人:Wesley Blake 提问时间:8/8/2023 更新时间:8/8/2023 访问量:23
PyGTK计算器
PyGTK calculator
问:
我想用 Python GTK 制作一个计算器。我正在尝试获取一个按钮来在应用程序中显示我的文本。但是,我的函数给了我这个错误。我知道 self 和参数,但我尝试过一个变量并传递到函数中,但它不起作用。TypeError: Calculator.clickedButton1() takes 1 positional argument but 2 were given
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()
答:
0赞
Wesley Blake
8/8/2023
#1
答案是将 self 传递到函数中。显然,自我并不像我所相信的那样被暗示。功劳归于@mkrieger1。
def clickedButton1(self, entry):
text = self.entry.get_text()
text += "1"
self.entry.set_text(text)
评论
def clickedButton1(self):