出于某种原因,以下代码可以正常工作,但如果计算机大约 10 分钟未使用(没有鼠标/键盘交互),则 ToolButton 文本将停止显示在面板上并且只是空白。奇怪的是,如果使用 PC 它将保持工作更长时间,任何想法为什么或什么可能导致这种不活动故障?
想想我的下一步可能是安装一个新的 Linux 虚拟机来测试代码,并排除我的安装没有尝试的想法。
还将尝试让代码从终端而不是 mate 桌面“添加到面板”或 mate-panel-test-applet 命令开始,因为我确信还有其他错误/警告没有通过打印语句打印由于是面板小程序。
#示例面板小程序输出文本:“/ = 6G” 我在用着: Linux katerina 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux Ubuntu 18.04.1 LTS GTK 3.22.30 python sys.version = '3.6.6 (default, Sep 12 2018, 18:26:19) \n[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
- 知道控件的工作方式可以获取点击事件
- 检查屏幕保护程序,电源管理选项
- 尝试在 ToolButton 上强制重绘事件
- 检查 ToolButton label.text 仍在更新
- 检查了我的磁盘空闲代码仍在工作,所以知道调度程序工作
- 检查更改磁盘空间文本 dd if=/dev/zero of=test bs=1024 count=1000000
尝试设置 box.set_above_child(False)
导入系统 导入日志 导入 logging.handlers 导入操作系统 进口舒蒂尔 导入 gi gi.require_version('MatePanelApplet', '4.0') gi.require_version("Gtk", "3.0") 从 gi.repository 导入 Gtk 从 gi.repository 导入 Gdk 从 gi.repository 导入 MatePanelApplet 从日期时间导入日期时间 从 apscheduler.scheduler.background 导入 BackgroundScheduler 从 hurry.filesize 导入大小
定义配置记录器(): #创建一个新的记录器并作为脚本调用连接到系统日志 my_logger = logging.getLogger("TestPythonApplet") my_logger.setLevel(logging.DEBUG) logging.getLogger('apscheduler').setLevel(logging.DEBUG) formatter = logging.Formatter('%(name)s[%(process)d]: %(levelname)s: %(message)s') handler = logging.handlers.SysLogHandler(address = '/dev/log') handler.setFormatter(格式化程序) my_logger.addHandler(处理程序) 返回 my_logger
<pre>
# create the applet. Basicly all your applet code for buttons, etc here
def applet_fill(applet):
#todo: maybe create a applet class like dock_applet.py example
my_logger.debug('Applet started')
#NOTE: Click events not always registered when click using ToolButton
#Think because it dont own its own X-window for preformance reasons as mostly decrotive
#As such dont normaly have to handle x events
#see: https://developer.gnome.org/gtkmm-tutorial/3.0/chapter-widgets-without-xwindows.html.en
#So we wrap in an Gtk.EventBox()
button = Gtk.ToolButton()
box = Gtk.EventBox() #Used to capture mouse events
box.set_above_child(True) #Fixes left clicks not being detected 100%
box.add(button)
box.connect("button_press_event", button_click, applet)
applet.add(box)
# show_image = False
# if show_image:
# #Follow works for showing image
# theme = Gtk.IconTheme().get_default()
# if theme.has_icon("mate"): #Keep things simple and look for known icon installed
# my_logger.debug('Mate icon found')
#
# image = Gtk.Image()
# #<class 'gi.repository.GdkPixbuf.Pixbuf
# i = theme.load_icon("happy", 32, Gtk.IconLookupFlags.NO_SVG)
# s= image.new_from_pixbuf(i) #<class 'gi.repository.Gtk.Image'>
# button.set_icon_widget(s)
# else: #Use text mode
# button.set_label(get_disk_free())
<pre>
button.set_label(get_disk_free())
#Now finnished creating applet start disk
scheduler.start()
scheduler.add_job(set_disk_free, 'interval', args=[applet], seconds=1, id='set_disk_free_id', replace_existing=True) #Args to pass to function set_disk_free
applet.show_all()
return True
</pre>
def set_disk_free(小程序): #TODO: 有没有办法applet.get_child_by_name("GtkToolButton") 类型函数 #查找 toolButton 小部件并设置文本 for items in applet.get_children(): # 获取applet下的子对象列表 #my_logger.debug(items.get_name()) # 应该是 'gi.repository.Gtk.EventBox' 如果 items.get_name() == "GtkEventBox": 对于 items.get_children() 中的项目: if items.get_name() == "GtkToolButton": #找到主工具按钮 items.set_label(get_disk_free()) items.queue_draw() #让我们出于某种原因尝试强制重绘小部件 #if dont touch pc for 10 min can't see text in panel #但是下面的调试行显示文本仍然设置和更新 my_logger.debug('标签:\"' + items.get_label() + '\"')
def get_disk_free():
usage_string = '/ = '+ size(shutil.disk_usage('/').free)
my_logger.debug('Updateing:' + str(datetime.now().strftime('%d-%m-%Y %H:%M:%S')) + usage_string )
return usage_string
def button_click(widget, event, applet):
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
my_logger.debug('Left Button clicked')
my_logger.debug('Updating free space text')
set_disk_free(applet)
if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
my_logger.debug('Right Button clicked')
def applet_factory(applet, iid, data):
# return False to tell mate we did not create a applet
if iid != "TestPythonApplet":
return False
applet_fill(applet)
return True
#Code starts here
my_logger = configure_logger()
scheduler = BackgroundScheduler()
MatePanelApplet.Applet.factory_main("TestPythonAppletFactory", True,
MatePanelApplet.Applet.__gtype__,
applet_factory, None)
my_logger.debug('Exiting')
scheduler.shutdown()
<pre>
#
#To test:
##########
##Create File: org.mate.panel.applet.TestPythonApplet.service
#[D-BUS Service]
#Name=org.mate.panel.applet.TestPythonAppletFactory
#Exec=/usr/bin/env python3 /home/chiptoxic/Downloads/mate-panel-applet/test_python_applet/test_python_applet.py
#
##Create File: org.mate.panel.TestPythonApplet.mate-panel-applet
#[Applet Factory]
#Id=TestPythonAppletFactory
#InProcess=false
#Location=/home/chiptoxic/Downloads/mate-panel-applet/test_python_applet/test_python_applet.py
#Name=Test Applet Factory
#Description=A MATE Python Applet example factory
#
#[TestPythonApplet]
#Name=Test Python Applet
#Description=A MATE Python Applet example
#Icon=happy
#MateComponentId=OAFIID:MATE_TestPythonApplet
#Simplified path to save space
#sudo ln -s org.mate.panel.TestPythonApplet.mate-panel-applet /usr/share/mate-panel/applets/org.mate.panel.TestPythonApplet.mate-panel-applet
#sudo ln -s org.mate.panel.applet.TestPythonApplet.service /usr/share/dbus-1/services/org.mate.panel.applet.TestPythonApplet.service
#chmod 755 org.mate.panel.TestPythonApplet.mate-panel-applet
#chmod 644 org.mate.panel.TestPythonApplet.service
#tail -f /var/log/syslog
#
#Following will kill process id of applet (have as bash script binded to key for easy testing / reload
#kill $(ps -aux | grep python3 | grep test_python_applet.py | tr -s ' ' | cut -d ' ' -f 2)
#
################################################################################
</pre>