如何使用wxPython制作窗口叠加层(在浏览器顶部,游戏除外) [英] How to make window overlay (on top of browser,games exc.) with wxPython

查看:142
本文介绍了如何使用wxPython制作窗口叠加层(在浏览器顶部,游戏除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的程序(此代码为演示),该程序将收集系统数据并将其显示在所有内容之上.我的目标是为当前最大的互联网用户创建一个总体ping 收集器.

I want to make a simple program (this code is a demo), that will collect system data and display it on top of everything. My goal is to create an overall ping collector for the current biggest internet user.

我所要的只是如何制作覆盖层.

All I'm asking for is how to make a overlay nothing more.

"""
Hello World, but with more meat.
"""

import wx

class HelloFrame(wx.Frame):
"""
A Frame that says Hello World
"""

def __init__(self, *args, **kw):
    # ensure the parent's __init__ is called
    super(HelloFrame, self).__init__(*args, **kw)

    # create a panel in the frame
    pnl = wx.Panel(self)

    # and put some text with a larger bold font on it
    st = wx.StaticText(pnl, label="Hello World!", pos=(25,25))
    font = st.GetFont()
    font.PointSize += 10
    font = font.Bold()
    st.SetFont(font)

    # create a menu bar
    self.makeMenuBar()

    # and a status bar
    self.CreateStatusBar()
    self.SetStatusText("Welcome to wxPython!")


def makeMenuBar(self):
    """
    A menu bar is composed of menus, which are composed of menu items.
    This method builds a set of menus and binds handlers to be called
    when the menu item is selected.
    """

    # Make a file menu with Hello and Exit items
    fileMenu = wx.Menu()
    # The "\t..." syntax defines an accelerator key that also triggers
    # the same event
    helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",
            "Help string shown in status bar for this menu item")
    fileMenu.AppendSeparator()
    # When using a stock ID we don't need to specify the menu item's
    # label
    exitItem = fileMenu.Append(wx.ID_EXIT)

    # Now a help menu for the about item
    helpMenu = wx.Menu()
    aboutItem = helpMenu.Append(wx.ID_ABOUT)

    # Make the menu bar and add the two menus to it. The '&' defines
    # that the next letter is the "mnemonic" for the menu item. On the
    # platforms that support it those letters are underlined and can be
    # triggered from the keyboard.
    menuBar = wx.MenuBar()
    menuBar.Append(fileMenu, "&File")
    menuBar.Append(helpMenu, "&Help")

    # Give the menu bar to the frame
    self.SetMenuBar(menuBar)

    # Finally, associate a handler function with the EVT_MENU event for
    # each of the menu items. That means that when that menu item is
    # activated then the associated handler function will be called.
    self.Bind(wx.EVT_MENU, self.OnHello, helloItem)
    self.Bind(wx.EVT_MENU, self.OnExit,  exitItem)
    self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)


def OnExit(self, event):
    """Close the frame, terminating the application."""
    self.Close(True)


def OnHello(self, event):
    """Say hello to the user."""
    wx.MessageBox("Hello again from wxPython")


def OnAbout(self, event):
    """Display an About Dialog"""
    wx.MessageBox("This is a wxPython Hello World sample",
                  "About Hello World 2",
                  wx.OK|wx.ICON_INFORMATION)


if __name__ == '__main__':
    # When this module is run (not imported) then create the app, the
    # frame, show it, and start the event loop.
    app = wx.App()
    frm = HelloFrame(None, title='Hello World 2')
    frm.Show()
    app.MainLoop()

这是我到目前为止用于ping收集的代码,它仍在制作过程中,显然,我必须对其进行一些修改并进行大量优化. 主题位于叠加层上,而不是此代码上.

And this is my code so far for the ping collecting "it is still in progress of making and obviously I'll have to modify it a bit + a lot of optimizing. The topic is on overlay not on this code.

import os
x= os.system('netstat -on > log.txt')
dat = open('log.txt','r')
line = dat.readlines()
dat.close()
list = []
line = line[4:] #removes irrelevant stuff

for x in line:
    y = ' '.join(x.split())
    if y != '':
        list.append(y)  #y[1:] tcp irrelevant but i'll keep it
for x in range(len(list)):
    list[x] = list[x].split(' ')

top = 0
for x in range(len(list)):
    count = 0
    for y in range(len(list)):
        if list[x][4] == list[y][4]:
            count= count+1
    if count > top:
        top = count
        ip = list[x]

ip = ''.join(ip[2].partition(':')[:1])

os.system('ping '+ip+' -n 3 > log.txt') # -n 3 repeat ping 3 times

dat = open('log.txt','r')
ping = dat.readlines()
dat.close()
ping = ping[len(ping)-1:]

print('Ping for ip: '+ip+' '+' '.join(ping[0].split()))
os.system('del log.txt') #useless stuff
input('')

推荐答案

This link is a good tutorial for frame styling/transparent overlays. Here is my favorite code from the tutorial, in which you make a transparent, movable, gray overlay:

import wx

class FancyFrame(wx.Frame):
    def __init__(self):
        style = ( wx.CLIP_CHILDREN | wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR |
                  wx.NO_BORDER | wx.FRAME_SHAPED  )
        wx.Frame.__init__(self, None, title='Fancy', style = style)
        self.Bind(wx.EVT_KEY_UP, self.OnKeyDown)
        self.Bind(wx.EVT_MOTION, self.OnMouse)
        self.SetTransparent( 220 )
        self.Show(True)

    def OnKeyDown(self, event):
        """quit if user press q or Esc"""
        if event.GetKeyCode() == 27 or event.GetKeyCode() == ord('Q'): #27 is Esc
            self.Close(force=True)
        else:
            event.Skip()

    def OnMouse(self, event):
        """implement dragging"""
        if not event.Dragging():
            self._dragPos = None
            return
        self.CaptureMouse()
        if not self._dragPos:
            self._dragPos = event.GetPosition()
        else:
            pos = event.GetPosition()
            displacement = self._dragPos - pos
            self.SetPosition( self.GetPosition() - displacement )

app = wx.App()
f = FancyFrame()
app.MainLoop()

以下是代码的输出:

PS:代码窗口不会显示在任务栏中

这篇关于如何使用wxPython制作窗口叠加层(在浏览器顶部,游戏除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆