gpt4 book ai didi

python - 如何在没有使用 python 的按键事件的情况下在内部更新/重绘 GTK 小部件 (GTKLabel)?

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:40 24 4
gpt4 key购买 nike

下面有一些代码试图更新 GTK Label 元素。我包括两个文件:ui 文件和 py 文件。

界面文件:

<glade-interface>
<widget class="GtkWindow" id="ApplicationFrame">
<property name="width_request">320</property>
<property name="height_request">240</property>
<property name="visible">True</property>
<property name="events">GDK_KEY_PRESS_MASK</property>
<property name="title" translatable="yes">Simple</property>
<property name="resizable">False</property>
<property name="window_position">center-always</property>
<property name="default_width">320</property>
<property name="default_height">240</property>
<property name="decorated">False</property>
<property name="gravity">center</property>
<child>
<widget class="GtkFixed" id="layout">
<property name="width_request">320</property>
<property name="height_request">240</property>
<property name="visible">True</property>
<child>
<widget class="GtkLabel" id="l1">
<property name="width_request">320</property>
<property name="height_request">40</property>
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">l1</property>
</widget>
<packing>
<property name="y">43</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>

Python文件

import os
from time import sleep as wait
import gtk as gtk
import gtk.glade as glade
import gobject

class Application(object):
def __init__ (self):
self.glade = glade.XML("simple.ui")
self.setup_ui()

def setup_ui (self):
self.window = self.glade.get_widget("ApplicationFrame")
self.l1 = self.glade.get_widget("l1")
self.names = {'l1' : self.l1}
self.all = [self.l1]
gobject.timeout_add(1000,self.display_me)
gobject.timeout_add(100,self.do_something_that_takes_a_while)
self.window.add_events(gtk.gdk.KEY_PRESS_MASK)
self.window.connect("key-press-event", self.handler)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.show()

def get_signal (self,widget,event):
keyname = gtk.gdk.keyval_name(event.keyval)
ctrl = event.state & gtk.gdk.CONTROL_MASK
alt = event.state & gtk.gdk.MOD1_MASK
shift = event.state & gtk.gdk.SHIFT_MASK
name = []
if ctrl and keyname not in ["Control_L","Control_R"]:
name.append("CTRL")
if alt and keyname not in ["Alt_L","Alt_R"]:
name.append("ALT")
if shift and keyname not in ["Shift_L","Shift_R"]:
name.append("SHIFT")
name.append(keyname)
name = "+".join(name)
return name

def handler (self,widget,event):
name = self.get_signal(widget,event)
if name.lower() in ['ctrl+x','ctrl+c','alt+q']:
self.destroy()

def main(self):
gtk.main()

def delete_event (self,widget=None,event=None,data=None):
return False

def destroy (self,widget=None,data=None):
gtk.main_quit()

def get (self,item):
if isinstance(item, str):
if item in self.names:
item = self.names[item]
retval = None
if hasattr(item,"text"):
retval = item.text()
elif hasattr(item,"get_label"):
retval = item.get_label()
return retval

def set (self,item,text='',font=None):
print 'Setting...'
if isinstance(item, str):
if item in self.names:
item = self.names[item]
retval = None
if font == None and hasattr(self,"page") and hasattr(self.page,"NORMAL_FONT"):
font = self.page.NORMAL_FONT
if hasattr(item,"setText"):
retval = item.setText(text)
elif font == None:
if hasattr(item,'set_text'):
retval = item.set_text(text)
elif hasattr(item,"set_label"):
retval = item.set_label(text)
if hasattr(item,"modify_font") and font != None:
item.modify_font(font)
item.queue_draw()
self.try_to_update(item)
print 'done'
return retval

def try_to_update (self,item):
"""
do something here to update the visual screen????
"""
print str(self)

def do_something_that_takes_a_while (self):
timeout = 15
while timeout != 0:
self.set('l1','%s' % timeout)
wait(1)
timeout -= 1
print timeout
return 1

def clean (self):
if item != None:
self.set(item,empty_text)
else:
if hasattr(self,'all'):
for item in self.all:
self.set(item)

def display_me (self):
print str(self)
return True

def __str__ (self):
space = 25
value = '%'+str(space)+'s'
lines = ['\n','-'*79]
if hasattr(self,'all'):
line = []
for obj in self.all:
obj_value = self.get(obj)
line.append(value % obj_value if obj_value != None else '')
#line.append(value % ' ')
lines.append(''.join(line))
lines.append('-'*79)
return '\n'.join(lines)


if __name__ == "__main__":
from time import sleep as wait
SEC = 1
app = Application()
app.main()

这应该很简单,但我完全不知道我做错了什么。元素 l1 未正确更新。我认为 try_to_update 中的代码是我需要更新 UI 的地方,但我不知道我需要调用哪个函数。有人可以帮忙吗?

提前致谢!

最佳答案

您需要使用小部件的 queue_draw功能:

The queue_draw_area() method invalidates the rectangular area of the widget (...) by calling the gtk.gdk.Window.invalidate_rect() method on the widget's window and all its child windows.

但是,由于您已经在您的 set 方法中这样做了,我的猜测是

if hasattr(item,'show')

check 阻止项目更新。另一个检查是在整个窗口上调用 queue_draw 函数,看看是否有更新。

或者,强制按如下方式处理排队的事件:

while gtk.events_pending():
gtk.main_iteration_do(True)

关于python - 如何在没有使用 python 的按键事件的情况下在内部更新/重绘 GTK 小部件 (GTKLabel)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8381631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com