吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 575|回复: 10
收起左侧

[求助] pytthon 对话框设置不会弄

[复制链接]
xuming98 发表于 2024-8-17 07:19
本帖最后由 xuming98 于 2024-8-17 07:22 编辑

本人小白,想设置一个循环执行N次的对话框,ai了几次都无法成功,请教各位老师如何编写
[Asm] 纯文本查看 复制代码
import pyautogui
import time
import win32gui
import win32con
import os
import sys
import subprocess



def switch_to_program(program_name):
    # 获取所有窗口的句柄
    hwnd_title = dict()

    def callback(hwnd, hwnd_title):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
            title = win32gui.GetWindowText(hwnd)
            hwnd_title[hwnd] = title

    win32gui.EnumWindows(callback, hwnd_title)

    # 在所有窗口标题中查找指定的程序名
    for hwnd, title in hwnd_title.items():
        if program_name in title:
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)  # 恢复窗口
            win32gui.SetForegroundWindow(hwnd)  # 切换焦点到该窗口
            break


# 使用示例
switch_to_program("Fiddler")  # 假设“记事本”是打开的程序名的一部分

# 按下Ctrl键
pyautogui.keyDown('ctrl')
# 等待一会儿,确保Ctrl键被识别为按下状态
time.sleep(0.1)

# 按下x键
pyautogui.keyDown('x')

# 释放x键
pyautogui.keyUp('x')



# 释放Ctrl键
pyautogui.keyUp('ctrl')
.
.
.

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| xuming98 发表于 2024-8-17 07:51
本帖最后由 xuming98 于 2024-8-17 08:02 编辑

比如这个,不知道为什么不弹窗,这个单独运行就弹,加入需要的语句就不弹
[Asm] 纯文本查看 复制代码
import tkinter as tk
import tkinter.simpledialog as sd
 
def run_loop():
    # 弹出对话框让用户输入循环次数
    loop_count = sd.askinteger("循环次数", "请输入循环次数:")
    if loop_count is not None:
        i = 0
        while i < loop_count:
            # 这里放置你想循环执行的代码
            print(f"循环次数:{i+1}/{loop_count}")
            i += 1
 
root = tk.Tk()
root.withdraw()  # 隐藏主窗口
 
run_loop()  # 运行循环函数
ahehaoyu 发表于 2024-8-17 08:35
你这个描述有点模糊,听得一头雾水   试试修改下问题描述,说不定有大佬能解答
黑泽心教 发表于 2024-8-17 09:13
测试代码如下:

from tkinter.dialogimport *

from  tkinterimport *

def MyDialog():

d = Dialog(None, title=“调查”, text=“喜欢Python编程吗?”, bitmap=“info”, default=0, strings=(“不喜欢”, “喜欢”, “非常喜欢”))

print(d.num)

btnStart = Button(None, text=“Python 调查”, command =MyDialog)

btnStart.pack()

btnQuit = Button(None, text=“关闭”, command=btnStart.quit)

btnQuit.pack()

btnStart.mainloop()

注意:弹出对话窗口的属性值设值,使用内置图标bitmap,内置图标实例,error,info,question,warning,hourglass等等,常见的logo。

ygq170063 发表于 2024-8-17 09:32
[Python] 纯文本查看 复制代码
import pyautogui
import time
import win32gui
import win32con
import tkinter as tk
from tkinter import simpledialog

def switch_to_program(program_name):
    hwnd_title = dict()

    def callback(hwnd, hwnd_title):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
            title = win32gui.GetWindowText(hwnd)
            hwnd_title[hwnd] = title

    win32gui.EnumWindows(callback, hwnd_title)

    for hwnd, title in hwnd_title.items():
        if program_name in title:
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
            win32gui.SetForegroundWindow(hwnd)
            break

def execute_task():
    switch_to_program("Fiddler")
    
    pyautogui.keyDown('ctrl')
    time.sleep(0.1)
    pyautogui.keyDown('x')
    pyautogui.keyUp('x')
    pyautogui.keyUp('ctrl')

def start_loop(n):
    for _ in range(n):
        execute_task()
        time.sleep(1)  # 根据需要调整间隔时间

def ask_loop_count():
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口

    # 弹出对话框,要求用户输入循环次数
    n = simpledialog.askinteger("输入次数", "请输入循环次数:")
    
    if n:
        start_loop(n)

    root.destroy()

# 开始运行
ask_loop_count()
Vampiremss 发表于 2024-8-17 12:40
不懂,等待其他大佬解答
269960696 发表于 2024-8-17 14:57
这个代码的主要功能是通过Python脚本自动切换到指定的程序窗口(如“Fiddler”),并模拟按下组合键(例如Ctrl + X)。以下是代码的主要部分及其功能解释:

导入必要的库:

pyautogui:用于模拟键盘和鼠标操作。
time:用于延迟操作。
win32gui 和 win32con:用于窗口操作,如获取窗口句柄、设置窗口焦点等。
os、sys、subprocess:用于与操作系统进行交互(这些在当前代码中没有被使用)。
switch_to_program 函数:

该函数通过遍历所有窗口,查找标题中包含指定程序名的窗口,并将该窗口恢复并切换到前台。
EnumWindows 枚举所有窗口,并通过回调函数获取每个窗口的句柄和标题。
找到匹配的窗口后,通过 ShowWindow 和 SetForegroundWindow 恢复并激活窗口。
模拟按键操作:

pyautogui.keyDown('ctrl') 和 pyautogui.keyUp('ctrl') 分别用于按下和释放Ctrl键。
pyautogui.keyDown('x') 和 pyautogui.keyUp('x') 分别用于按下和释放X键。
time.sleep(0.1) 是为了确保按键之间有一定的延迟,避免按键顺序被误识别。
改进和注意事项:
错误处理:

如果未找到指定的窗口,代码不会给出反馈,可能会导致后续操作失败。可以添加提示或日志记录以便调试。
多窗口问题:

如果有多个窗口的标题包含相同的名称,这个代码可能会切换到意外的窗口。可以考虑改进匹配逻辑,例如更精确地匹配窗口标题。
键盘操作延迟:

根据系统性能和应用响应速度,可以调整 time.sleep() 的延迟时间以确保操作的稳定性
 楼主| xuming98 发表于 2024-8-17 15:51
本帖最后由 xuming98 于 2024-8-17 15:56 编辑
ygq170063 发表于 2024-8-17 09:32

[Asm] 纯文本查看 复制代码
import pyautogui
import time
import win32gui
import win32con
import os
import sys
import subprocess

def switch_to_program(program_name):
    # 获取所有窗口的句柄
    hwnd_title = dict()

    def callback(hwnd, hwnd_title):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
            title = win32gui.GetWindowText(hwnd)
            hwnd_title[hwnd] = title

    win32gui.EnumWindows(callback, hwnd_title)

    # 在所有窗口标题中查找指定的程序名
    for hwnd, title in hwnd_title.items():
        if program_name in title:
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)  # 恢复窗口
            win32gui.SetForegroundWindow(hwnd)  # 切换焦点到该窗口
            break


# 使用示例
switch_to_program("Fiddler")  # 假设“记事本”是打开的程序名的一部分

# 按下Ctrl键
pyautogui.keyDown('ctrl')
# 等待一会儿,确保Ctrl键被识别为按下状态
time.sleep(0.1)

# 按下x键
pyautogui.keyDown('x')

# 释放x键
pyautogui.keyUp('x')



# 释放Ctrl键
pyautogui.keyUp('ctrl')

def switch_to_program(program_name):
    # 获取所有窗口的句柄
    hwnd_title = dict()

    def callback(hwnd, hwnd_title):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
            title = win32gui.GetWindowText(hwnd)
            hwnd_title[hwnd] = title

    win32gui.EnumWindows(callback, hwnd_title)

    # 在所有窗口标题中查找指定的程序名
    for hwnd, title in hwnd_title.items():
        if program_name in title:
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)  # 恢复窗口
            win32gui.SetForegroundWindow(hwnd)  # 切换焦点到该窗口
            break


# 使用示例
switch_to_program("装配答题")  # 假设“记事本”是打开的程序名的一部分
pyautogui.click(1500, 725)

def switch_to_program(program_name):
    # 获取所有窗口的句柄
    hwnd_title = dict()

    def callback(hwnd, hwnd_title):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
            title = win32gui.GetWindowText(hwnd)
            hwnd_title[hwnd] = title

    win32gui.EnumWindows(callback, hwnd_title)

    # 在所有窗口标题中查找指定的程序名
    for hwnd, title in hwnd_title.items():
        if program_name in title:
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)  # 恢复窗口
            win32gui.SetForegroundWindow(hwnd)  # 切换焦点到该窗口
            break


# 使用示例
switch_to_program("Fiddler")  # 假设“记事本”是打开的程序名的一部分

# 延时1秒
time.sleep(1)

# 移动鼠标到坐标(x=600, y=125)
pyautogui.moveTo(x=600, y=125)

# 执行鼠标右键点击
pyautogui.click(button='right')
pyautogui.click(700, 260)
pyautogui.click(950, 330)

def close_notepad():
    # 使用任务管理器结束记事本进程
    subprocess.run(['taskkill', '/im', 'notepad.exe', '/f'])


if __name__ == "__main__":
    close_notepad()

# 执行左键双击
pyautogui.click(115, 915, button='left', clicks=2)




非常感谢!完美的运行!

能不能再给整理完整代码,我再您的代码里继续添加后续代码最后还是失败,给您添麻烦了
cfnm123 发表于 2024-8-17 16:27
[Asm] 纯文本查看 复制代码
import pyautogui
import time
import win32gui
import win32con

def switch_to_program(program_name):
    """
    切换到指定名称的应用程序窗口。
    
    参数:
        program_name (str): 应用程序的窗口标题中应包含的部分文本。
    """
    # 获取所有窗口的句柄
    hwnd_title = dict()

    def callback(hwnd, hwnd_title):
        if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd):
            title = win32gui.GetWindowText(hwnd)
            hwnd_title[hwnd] = title

    win32gui.EnumWindows(callback, hwnd_title)

    # 在所有窗口标题中查找指定的程序名
    for hwnd, title in hwnd_title.items():
        if program_name in title:
            win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)  # 恢复窗口
            win32gui.SetForegroundWindow(hwnd)  # 切换焦点到该窗口
            break

def simulate_ctrl_x():
    """
    模拟按下 Ctrl+X 键组合。
    """
    pyautogui.hotkey('ctrl', 'x')

def execute_action_n_times(n, action, delay=0.5):
    """
    执行给定的动作 n 次,并在每次动作之间插入一个可选的延迟。
    
    参数:
        n (int): 要执行的次数。
        action (function): 要执行的动作。
        delay (float): 动作之间的延迟时间(秒)。
    """
    for _ in range(n):
        action()
        time.sleep(delay)

def main():
    n = 5  # 要执行的次数
    program_name = "Fiddler"

    # 切换到目标程序
    switch_to_program(program_name)

    # 执行模拟 Ctrl+X 的动作 n 次
    execute_action_n_times(n, simulate_ctrl_x)

if __name__ == "__main__":
    main()
知心 发表于 2024-8-17 16:29
xuming98 发表于 2024-8-17 07:51
比如这个,不知道为什么不弹窗,这个单独运行就弹,加入需要的语句就不弹[mw_shl_code=asm,true]import tki ...

循环占用了主线程,需要开多线程
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-21 11:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表