python Kivy窗口隐藏/显示

5jdjgkvh  于 2023-02-28  发布在  Python
关注(0)|答案(2)|浏览(210)

我是一个与python编程和tought新手,学习让我创建一个项目。这里是我正在尝试做的。我想创建一个程序,在系统托盘和火的程序是在后台加载运行。加载在后台,所以我可以减少Kivy的启动时间。在这里和谷歌搜索后,我找不到答案。
我有两个文件

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.config import Config
# 

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window

Config.set('graphics', 'window_state', 'hidden')

class MyApp(App):
    visible = False

    def build(self):
        Window.bordeless = 'True'
        return Button(text='Hello World')

    def on_start(self):
        if self.visible:
            self.root_window.hide()
        self.visible = not self.visible
        # self.root.focus = True

    def do_show(self):
        rootWindow = self.root_window
        rootWindow.show()
        print(self.root_window.focus)

if __name__ in ('__main__'):
    ma = MyApp().run()

以及

import wx
import someKivyThigy
from kivy.app import App
from someKivyThigy import MyApp
from buttonThing import MyDebugApp
from kivy.core.window import Window

TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'icon.png'

testVar = MyApp

def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.AppendItem(item)
    return item

class TaskBarIcon(wx.TaskBarIcon):
    def __init__(self):
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)

    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Say Hello', self.on_hello)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu

    def set_icon(self, path):
        icon = wx.IconFromBitmap(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)

    def on_left_down(self, event):
        print 'Tray icon was left-clicked.'
        MyApp().do_show()

    def on_hello(self, event):
        print 'Hello, world!'

    def on_exit(self, event):
        wx.CallAfter(self.Destroy)

def main():
    app = wx.PySimpleApp()
    TaskBarIcon()
    testVar = MyApp().run()
    app.MainLoop()

if __name__ == '__main__':
    main()

当我左击系统托盘图标时,出现错误:"属性错误:"NoneType"对象没有属性"show""。我做错了什么?

aiazj4mn

aiazj4mn1#

Kivy是专门为Android平台而设计的,但这并不意味着它不能用于桌面应用程序开发。但请记住,窗口管理器与保持SLD2提供程序作为备份工作,因此,该提供程序支持隐藏窗口,但不能恢复它。
要隐藏窗口,可以用途:从kivy.core.window导入窗口后使用Window.hide()
要进行恢复,您可以用途:Windows.还原()或Window.show()

r55awzrz

r55awzrz2#

对我来说,解决方案只是在其他导入之前设置config

from kivy.config import Config
Config.set('graphics', 'window_state', 'hidden')

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window

相关问题