在 Python 中按下按钮期间的动作
Action during button pressed in Python
在使用 Python 进行 GUI 编程时,我是一个初学者,所以我猜我的问题有一个简单的解决方案,即使我没有找到解决方案搜索 google、wikis、文档, 例子 ...
我想要实现的是,只要按下按钮,条目小部件中的值就应该不断增加到最大值。
我已尝试对事件 "button-press-event" 使用回调,但得到的响应与对信号 "clicked" 的回调相同。
当连接到 "clicked" 时,应用程序按预期工作,例如每次点击该值都会增加,但是当我使用与事件的连接时 "button-press-event" 我仍然只会在每次点击(按下并释放)时获得增量
我将它与 Python3.2 一起使用(在 Raspberry Pi 2 上)
以下是作用于每次点击的工作代码:
#!/usr/bin/python
from gi.repository import Gtk, Gdk
from time import sleep
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ST")
MAX_SPD = 3000
self.set_default_size(100,100)
self.set_size_request(100,100)
self.set_resizable(False)
spdEntry = Gtk.Entry()
spdEntry.set_has_frame(True)
spdEntry.set_text("0")
spdEntry.connect("activate", self.inputSpd, MAX_SPD)
start = Gtk.Button("Start")
start.connect("clicked", self.clicked_start)
stop = Gtk.Button("Stop")
stop.connect("clicked", self.clicked_stop)
inc = Gtk.Button("inc")
inc.connect("clicked", self.pressed_inc, spdEntry, MAX_SPD)
fixed = Gtk.Fixed()
fixed.put(start, 0, 0)
fixed.put(spdEntry, 0, 40)
fixed.put(stop, 0, 70)
fixed.put(inc, 120, 0)
self.add(fixed)
def clicked_start(self, widget):
self.set_title("GO")
def clicked_stop(self, widget):
self.set_title("ST")
def pressed_inc(self, widget, entry, maxSpd):
speed = int(entry.get_text())
speed = speed+1
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
def inputSpd(self, entry, maxSpd):
speed = int(entry.get_text())
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
这是非工作代码,当长按按钮时应该递增:
#!/usr/bin/python
from gi.repository import Gtk, Gdk
from time import sleep
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ST")
MAX_SPD = 3000
self.set_default_size(100,100)
self.set_size_request(100,100)
self.set_resizable(False)
spdEntry = Gtk.Entry()
spdEntry.set_has_frame(True)
spdEntry.set_text("0")
spdEntry.connect("activate", self.inputSpd, MAX_SPD)
start = Gtk.Button("Start")
start.connect("clicked", self.clicked_start)
stop = Gtk.Button("Stop")
stop.connect("clicked", self.clicked_stop)
inc = Gtk.Button("inc")
inc.connect("button-press-event", self.pressed_inc, spdEntry, MAX_SPD)
inc.connect("button-release-event", self.left_inc)
fixed = Gtk.Fixed()
fixed.put(start, 0, 0)
fixed.put(spdEntry, 0, 40)
fixed.put(stop, 0, 70)
fixed.put(inc, 120, 0)
self.add(fixed)
def clicked_start(self, widget):
self.set_title("GO")
def clicked_stop(self, widget):
self.set_title("ST")
def pressed_inc(self, widget, event, entry, maxSpd):
if event.type == Gdk.EventType.BUTTON_PRESS:
speed = int(entry.get_text())
speed = speed+1
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
return True
def left_inc(self, widget, event):
if event.type == Gdk.EventType.BUTTON_RELEASE:
return True
def inputSpd(self, entry, maxSpd):
speed = int(entry.get_text())
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
您可以使用 GLib.timeout_add()
来完成此操作。但是,我同意 gianmt 最好只使用 Gtk.SpinButton
#!/usr/bin/python
from gi.repository import Gtk, Gdk, GLib
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ST")
self.MAXSPEED = 3000
self.set_default_size(100,100)
self.set_resizable(False)
self.entry = Gtk.Entry()
self.entry.set_text("0")
start = Gtk.Button(label="Start")
start.connect("clicked", self.clicked_start)
stop = Gtk.Button(label="Stop")
stop.connect("clicked", self.clicked_stop)
inc = Gtk.Button(label="inc")
inc.connect("button-press-event", self.pressed_inc)
inc.connect("button-release-event", self.left_inc)
fixed = Gtk.Fixed()
fixed.put(start, 0, 0)
fixed.put(self.entry, 0, 40)
fixed.put(stop, 0, 70)
fixed.put(inc, 120, 0)
self.add(fixed)
def clicked_start(self, widget):
self.set_title("GO")
def clicked_stop(self, widget):
self.set_title("ST")
def pressed_inc(self, widget, event):
self.inc_id = GLib.timeout_add(100, self.increase)
def left_inc(self, widget, event,):
GLib.source_remove(self.inc_id)
def increase(self):
speed = int(self.entry.get_text()) + 1
if speed > self.MAXSPEED:
self.entry.set_text(str(self.MAXSPEED))
else:
self.entry.set_text(str(speed))
return True
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
在您的代码之上还有一个 from time import sleep
作为导入。它没有被使用,我不知道你的应用程序的其余部分,但是当你不在线程中时使用 time.sleep()
,将阻塞 Gtk.main()
循环并使你的应用程序无响应。
在使用 Python 进行 GUI 编程时,我是一个初学者,所以我猜我的问题有一个简单的解决方案,即使我没有找到解决方案搜索 google、wikis、文档, 例子 ...
我想要实现的是,只要按下按钮,条目小部件中的值就应该不断增加到最大值。 我已尝试对事件 "button-press-event" 使用回调,但得到的响应与对信号 "clicked" 的回调相同。 当连接到 "clicked" 时,应用程序按预期工作,例如每次点击该值都会增加,但是当我使用与事件的连接时 "button-press-event" 我仍然只会在每次点击(按下并释放)时获得增量
我将它与 Python3.2 一起使用(在 Raspberry Pi 2 上)
以下是作用于每次点击的工作代码:
#!/usr/bin/python
from gi.repository import Gtk, Gdk
from time import sleep
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ST")
MAX_SPD = 3000
self.set_default_size(100,100)
self.set_size_request(100,100)
self.set_resizable(False)
spdEntry = Gtk.Entry()
spdEntry.set_has_frame(True)
spdEntry.set_text("0")
spdEntry.connect("activate", self.inputSpd, MAX_SPD)
start = Gtk.Button("Start")
start.connect("clicked", self.clicked_start)
stop = Gtk.Button("Stop")
stop.connect("clicked", self.clicked_stop)
inc = Gtk.Button("inc")
inc.connect("clicked", self.pressed_inc, spdEntry, MAX_SPD)
fixed = Gtk.Fixed()
fixed.put(start, 0, 0)
fixed.put(spdEntry, 0, 40)
fixed.put(stop, 0, 70)
fixed.put(inc, 120, 0)
self.add(fixed)
def clicked_start(self, widget):
self.set_title("GO")
def clicked_stop(self, widget):
self.set_title("ST")
def pressed_inc(self, widget, entry, maxSpd):
speed = int(entry.get_text())
speed = speed+1
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
def inputSpd(self, entry, maxSpd):
speed = int(entry.get_text())
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
这是非工作代码,当长按按钮时应该递增:
#!/usr/bin/python
from gi.repository import Gtk, Gdk
from time import sleep
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ST")
MAX_SPD = 3000
self.set_default_size(100,100)
self.set_size_request(100,100)
self.set_resizable(False)
spdEntry = Gtk.Entry()
spdEntry.set_has_frame(True)
spdEntry.set_text("0")
spdEntry.connect("activate", self.inputSpd, MAX_SPD)
start = Gtk.Button("Start")
start.connect("clicked", self.clicked_start)
stop = Gtk.Button("Stop")
stop.connect("clicked", self.clicked_stop)
inc = Gtk.Button("inc")
inc.connect("button-press-event", self.pressed_inc, spdEntry, MAX_SPD)
inc.connect("button-release-event", self.left_inc)
fixed = Gtk.Fixed()
fixed.put(start, 0, 0)
fixed.put(spdEntry, 0, 40)
fixed.put(stop, 0, 70)
fixed.put(inc, 120, 0)
self.add(fixed)
def clicked_start(self, widget):
self.set_title("GO")
def clicked_stop(self, widget):
self.set_title("ST")
def pressed_inc(self, widget, event, entry, maxSpd):
if event.type == Gdk.EventType.BUTTON_PRESS:
speed = int(entry.get_text())
speed = speed+1
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
return True
def left_inc(self, widget, event):
if event.type == Gdk.EventType.BUTTON_RELEASE:
return True
def inputSpd(self, entry, maxSpd):
speed = int(entry.get_text())
if speed > maxSpd:
speed = maxSpd
entry.set_text(str(speed))
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
您可以使用 GLib.timeout_add()
来完成此操作。但是,我同意 gianmt 最好只使用 Gtk.SpinButton
#!/usr/bin/python
from gi.repository import Gtk, Gdk, GLib
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ST")
self.MAXSPEED = 3000
self.set_default_size(100,100)
self.set_resizable(False)
self.entry = Gtk.Entry()
self.entry.set_text("0")
start = Gtk.Button(label="Start")
start.connect("clicked", self.clicked_start)
stop = Gtk.Button(label="Stop")
stop.connect("clicked", self.clicked_stop)
inc = Gtk.Button(label="inc")
inc.connect("button-press-event", self.pressed_inc)
inc.connect("button-release-event", self.left_inc)
fixed = Gtk.Fixed()
fixed.put(start, 0, 0)
fixed.put(self.entry, 0, 40)
fixed.put(stop, 0, 70)
fixed.put(inc, 120, 0)
self.add(fixed)
def clicked_start(self, widget):
self.set_title("GO")
def clicked_stop(self, widget):
self.set_title("ST")
def pressed_inc(self, widget, event):
self.inc_id = GLib.timeout_add(100, self.increase)
def left_inc(self, widget, event,):
GLib.source_remove(self.inc_id)
def increase(self):
speed = int(self.entry.get_text()) + 1
if speed > self.MAXSPEED:
self.entry.set_text(str(self.MAXSPEED))
else:
self.entry.set_text(str(speed))
return True
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
在您的代码之上还有一个 from time import sleep
作为导入。它没有被使用,我不知道你的应用程序的其余部分,但是当你不在线程中时使用 time.sleep()
,将阻塞 Gtk.main()
循环并使你的应用程序无响应。