因此,我将数字写在条目中,当我按下按钮时,在标签中应显示我的值乘以10。但是,当我按下按钮时,我得到此错误:'str'对象没有属性'get_text '。 我不知道该怎么做。请帮助:(
import gi gi.require_version('Gtk','3.0') 从gi.repository导入Gtk
EntryWindow类(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Entry Demo")
self.set_default_size(200, 100)
grid = Gtk.Grid()
self.add(grid)
self.entry1 = Gtk.Entry()
self.entry1.set_activates_default(True)
self.t1 = Gtk.Label()
button = Gtk.Button(label="CALC")
button.connect("clicked", self.calc)
grid.attach(self.entry1, 0, 1, 1, 1)
grid.attach(self.t1, 1,2,1,1)
grid.attach(button, 2,1,1,1)
def calc(self, widget, data=None):
port = self.entry1.get_text()
a = float(port.get_text())*10
self.t1.set_text(" " + a.get_text())
答案 0 :(得分:0)
我已经做到了。我不知道这是否是最好的解决方案,但是就是这样:(如果有人有更好的解决方案,我会全力以赴) def init (自己): Gtk.Window。初始化(自身,标题=“更改标签文本”)
table = Gtk.Table(3, 3, True)
self.add(table)
adjustment = Gtk.Adjustment(0, 0, 100, 1, 10, 0)
self.spinbutton = Gtk.SpinButton()
self.spinbutton.set_adjustment(adjustment)
self.label = Gtk.Label("Press the button")
button = Gtk.Button("Press Me")
button.connect("clicked", self.button_pressed)
table.attach(self.label, 1, 2, 0, 1)
table.attach(button, 1, 2, 1, 2)
table.attach(self.spinbutton, 1, 2 ,2 ,3)
def button_pressed(self, button):
port = self.spinbutton.get_value()
self.label.set_text(str(int(port)*10))