if name == "main":
app = QApplication(sys.argv)
window = DraggableWindow()
window.show()
sys.exit(app.exec())。。。。。。。。用wxPython库重写这个程序
Certainly! Below is a comprehensive guide to rewriting your PySide6 application using wxPython. This wxPython version replicates the functionality of your original script, providing two buttons for scrolling up and down when right-clicked and held, along with a draggable, frameless, always-on-top window that does not grab focus.
Overview
GUI Framework: wxPython
Mouse Control: pynput library for simulating mouse scrolls
Main Features:
Frameless, always-on-top window
Two buttons for scrolling up and down
Right-click and hold on buttons to enable continuous scrolling
Draggable window via left mouse button without grabbing focus
Prerequisites
Ensure you have the necessary libraries installed:
bash
pip install wxPython pynput
Note: Installing wxPython might require additional steps depending on your operating system. Refer to the official wxPython installation guide if you encounter issues.
Complete wxPython Implementation
python
import sys
import ctypes
import platform
import wx
from pynput.mouse import Controller as MouseController
from threading import Thread, Event
import time
# Windows-specific constants and functions
if platform.system() == "Windows":
GWL_EXSTYLE = -20
WS_EX_NOACTIVATE = 0x08000000
WS_EX_TOOLWINDOW = 0x00000080
def set_window_noactivate(hwnd):
ex_style = ctypes.windll.user32.GetWindowLongW(hwnd, GWL_EXSTYLE)
ex_style |= WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW
ctypes.windll.user32.SetWindowLongW(hwnd, GWL_EXSTYLE, ex_style)
class ScrollButton(wx.Button):
def __init__(self, parent, label, direction):
super().__init__(parent, label=label, style=wx.BU_EXACTFIT)
self.direction = direction # 'up' or 'down'
self.mouse = MouseController()
self.scroll_thread = None
self.stop_event = Event()
# Bind mouse events
self.Bind(wx.EVT_RIGHT_DOWN, self.on_right_down)
self.Bind(wx.EVT_RIGHT_UP, self.on_right_up)
self.Bind(wx.EVT_LEAVE_WINDOW, self.on_right_up) # Stop scrolling if cursor leaves
# Set button styles
self.SetBackgroundColour(wx.Colour(52, 152, 219, 204)) # RGBA
self.SetForegroundColour(wx.WHITE)
self.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
self.SetWindowStyleFlag(wx.BORDER_NONE)
self.SetMinSize((100, 50))
def on_right_down(self, event):
self.stop_event.clear()
self.scroll_thread = Thread(target=self.scroll_continuously)
self.scroll_thread.start()
event.Skip()
def on_right_up(self, event):
self.stop_event.set()
if self.scroll_thread and self.scroll_thread.is_alive():
self.scroll_thread.join()
event.Skip()
def scroll_continuously(self):
while not self.stop_event.is_set():
if self.direction == 'up':
self.mouse.scroll(0, 1) # Scroll up
elif self.direction == 'down':
self.mouse.scroll(0, -1) # Scroll down
time.sleep(0.05) # Adjust scroll speed here (in seconds)
class DraggableFrame(wx.Frame):
def __init__(self):
# Window styles to make it frameless and always on top
style = wx.FRAME_NO_TASKBAR | wx.STAY_ON_TOP | wx.FRAME_SHAPED | wx.NO_BORDER
super().__init__(parent=None, title="", style=style)
# Make window transparent if desired
self.SetTransparent(230) # 0 (fully transparent) to 255 (fully opaque)
# Initialize UI
self.init_ui()
# Variables for dragging
self.dragging = False
self.drag_position = (0, 0)
# Platform-specific settings
if platform.system() == "Windows":
hwnd = self.GetHandle()
set_window_noactivate(hwnd)
elif platform.system() == "Darwin":
# macOS-specific settings can be handled here
pass
elif platform.system() == "Linux":
# Linux-specific settings can be handled here
pass
# Bind mouse events for dragging
self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
self.Bind(wx.EVT_LEFT_UP, self.on_left_up)
self.Bind(wx.EVT_MOTION, self.on_motion)
self.Bind(wx.EVT_PAINT, self.on_paint)
def init_ui(self):
panel = wx.Panel(self, style=wx.BORDER_NONE)
vbox = wx.BoxSizer(wx.VERTICAL)
# Create Up and Down buttons
up_button = ScrollButton(panel, "⬆️ 上滚", 'up')
down_button = ScrollButton(panel, "⬇️ 下滚", 'down')
vbox.Add(up_button, 0, wx.ALL | wx.EXPAND, 5)
vbox.Add(down_button, 0, wx.ALL | wx.EXPAND, 5)
panel.SetSizer(vbox)
self.SetSize((120, 120)) # Adjust window size as needed
self.Centre()
def on_left_down(self, event):
self.dragging = True
x, y = event.GetPosition()
self.drag_position = (x, y)
self.CaptureMouse()
def on_left_up(self, event):
if self.dragging:
self.dragging = False
self.ReleaseMouse()
def on_motion(self, event):
if self.dragging and event.Dragging() and event.LeftIsDown():
x, y = self.ClientToScreen(event.GetPosition())
new_x = x - self.drag_position[0]
new_y = y - self.drag_position[1]
self.Move((new_x, new_y))
def on_paint(self, event):
# Optional: Customize window appearance
dc = wx.PaintDC(self)
dc.SetBrush(wx.Brush(wx.Colour(52, 152, 219, 204)))
dc.SetPen(wx.TRANSPARENT_PEN)
dc.DrawRoundedRectangle(0, 0, self.GetSize().width, self.GetSize().height, 10)
def ShowWithoutActivate(self):
# Platform-specific method to show window without activating
if platform.system() == "Windows":
HWND_TOPMOST = -1
SWP_NOSIZE = 0x0001
SWP_NOMOVE = 0x0002
SWP_NOACTIVATE = 0x0010
SWP_SHOWWINDOW = 0x0040
hwnd = self.GetHandle()
ctypes.windll.user32.SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW)
else:
self.Show()
class MyApp(wx.App):
def OnInit(self):
self.frame = DraggableFrame()
self.frame.ShowWithoutActivate()
return True
if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()
Explanation of the wxPython Implementation
1. Window Creation and Styles
Frameless and Always on Top: The DraggableFrame class inherits from wx.Frame with styles wx.FRAME_NO_TASKBAR, wx.STAY_ON_TOP, wx.FRAME_SHAPED, and wx.NO_BORDER to create a frameless, always-on-top window without appearing in the taskbar.
Transparency: self.SetTransparent(230) sets the window's transparency. Adjust the alpha value (0-255) as needed.
Platform-Specific No Activate:
Windows: Uses the SetWindowLongW function from the user32 DLL to add styles WS_EX_NOACTIVATE and WS_EX_TOOLWINDOW, ensuring the window does not activate or appear in the taskbar.
macOS and Linux: Placeholders are provided where platform-specific code can be added if needed.
2. Buttons with Continuous Scrolling
ScrollButton Class:
Inherits from wx.Button.
Handles EVT_RIGHT_DOWN to start scrolling and EVT_RIGHT_UP or EVT_LEAVE_WINDOW to stop scrolling.
Uses a separate thread to perform continuous scrolling while the right mouse button is held down.
Scrolls using the pynput library’s MouseController.
Button Styles:
Background color, text color, font, and size are set to resemble the original PySide6 buttons.
Adjust SetMinSize as needed.
3. Draggable Window
Mouse Events:
Left Mouse Button Press (EVT_LEFT_DOWN): Initiates dragging by capturing the mouse and storing the drag position.
Left Mouse Button Release (EVT_LEFT_UP): Ends dragging by releasing the mouse capture.
Mouse Motion (EVT_MOTION): If dragging, calculates and moves the window based on the mouse position.
Painting the Window:
EVT_PAINT: Optionally, customizes the window appearance by drawing a rounded rectangle. Modify or remove this method to change the window's visual style.
4. Showing the Window Without Activation
ShowWithoutActivate Method:
Windows: Uses SetWindowPos to show the window without activating it.
Other Platforms: Calls self.Show() as a placeholder. You may need to implement platform-specific functionality if required.
5. Main Application Loop
MyApp Class:
Inherits from wx.App.
Initializes and shows the DraggableFrame without activating it.
Running the Application:
The application starts by creating an instance of MyApp and entering the main event loop with app.MainLoop().
Customization and Enhancements
Adjusting Scroll Speed: Modify the time.sleep(0.05) value in the scroll_continuously method to increase or decrease the scroll speed.
Window Appearance: Customize the on_paint method in the DraggableFrame class to change the window’s shape, color, and other visual aspects.
Cross-Platform Considerations:
macOS: Making a window non-activating is more complex and may require integrating with macOS-specific APIs.
Linux: Behavior might vary depending on the window manager. Additional flags or methods might be necessary.
Error Handling: Implement necessary error handling, especially around thread management and platform-specific calls.
Conclusion
This wxPython implementation closely mirrors the functionality of your original PySide6 application, providing a lightweight, always-on-top, and draggable window with buttons for continuous scrolling via right-click. Adjust and extend the code as needed to fit your specific requirements and handle any platform-specific nuances.
Feel free to reach out if you have any questions or need further assistance!