无法使用Win32gui和Windows_Cursers

问题描述 投票:0回答:1

我正在尝试用Python制作一个授权King James圣经的阅读应用程序!

我在人工智能的帮助下用一些字体和东西创建了脚本。虽然当我尝试运行它时,它说 win32gui,win32gui 未安装。

我将 Python 降级到 3.6.2 并且能够安装它。虽然还是说找不到。

我正在使用 Pyinstaller 和 Auto-Py-To-Exe 进行转换。

我尝试使用虚拟机并降级Python。

import os
import ctypes
import colorama
from colorama import Style, Fore, Back, init
from rich.console import Console
import shutil
import windows_curses
import sys
import win32gui
import pyautogui
import argparse
from pynput.keyboard import Controller, Key
import textwrap
import keyboard
import time
import subprocess


# Detect the environment (terminal or cmd)
if sys.stdout.isatty():
    # Use the hotkey for **Windows Terminal**
    pyautogui.hotkey('win', 'up')

#Get the console window
hWnd = ctypes.windll.kernel32.GetConsoleWindow()
size = os.get_terminal_size()

def get_window_position(hwnd):
    rect = win32gui.GetWindowRect(hwnd)
    x, y, width, height = rect
    return x, y

# Get the handle of the active window
active_window = win32gui.GetForegroundWindow()

# Get the position of the active window
x_pos, y_pos = get_window_position(active_window)

# Title page
columns = shutil.get_terminal_size().columns

print("Authorized King James Bible".center(columns))
print("Thy word is a lamp unto my feet, and a light unto my path.".center(columns))
print("- Psalms 119:105".center(columns))

# Genesis title and chapter 1
print("\n" + Fore.CYAN + "THE FIRST BOOK OF MOSES CALLED GENESIS" + Fore.RESET + "\n")
print(Fore.GREEN + "Chapter 1" + Fore.RESET + "\n")

    # Genesis chapter 1 verses
    
    #Day 1
    # Get the terminal size
    size = os.get_terminal_size()
    # Use the columns attribute as the width
    print(textwrap.fill("1:1 ¶ IN the beginning God created the heaven and the earth.", width=size.columns)) 
    print(textwrap.fill("1:2 And the earth was without form, and void; and darkness was upon the face of the waters.", width=size.columns))
    print(textwrap.fill("1:3 And God said, Let there be light: and there was light.", width=size.columns))
    print(textwrap.fill("1:4 And God saw the light, that \x1B[3mit was\x1B[0m good: and God divided the light from the darkness.", width=size.columns))
    print(textwrap.fill("1:5 And God called the light Day, and the darkness he called Night. And the evening and morning were the first day.", width=size.columns))
    print("\n")
    
    # Pause the script
    time.sleep(0.2)
    
    # Create an argument parser
    parser = argparse.ArgumentParser()
    parser.add_argument("--env", help="Specify the environment (terminal or cmd)", choices=['terminal', 'cmd'])
    
    # Parse the arguments
    args = parser.parse_args()
    
    # Create a keyboard controller
    keyboard = Controller()
    
    # Use the hotkey for Windows Terminal
    if args.env == 'terminal' or sys.stdout.isatty():
        with keyboard.pressed(Key.ctrl, Key.shift): 
            keyboard.press(Key.home)
            keyboard.release(Key.home)
    else:
        # Use the hotkey for CMD
        with keyboard.pressed(Key.ctrl):
            keyboard.press(Key.home)
            keyboard.release(Key.home)
            
    # Pause the script
    input("\n" + "Press Alt-Enter to Exit out of Full Screen Mode or Press Enter to exit..." + "\n")
python
1个回答
0
投票

您最好的选择是采用完全不同的方法 - win32gui 是一个与您的系统交互的极低级别的管理器。我知道你使用 AI 来生成这个,所以你可能对 Python 经验较少。您提供的程序只是将前几节内容打印到终端中,同时为用户提供了一些关闭应用程序的控制权。

第一件事:您将需要您尝试使用的圣经的 .txt 副本。 我在此链接中找到了一个:https://openbible.com/textfiles/kjv.txt [验证其是否正确]。 鉴于您的圣经文本文件的格式为: 书籍章节:VERSE LOREM IPSUM ECT ECT 然后,您可以轻松地解析文本文件,为其建立索引,并将书籍拆分为单独的对象。然后,您可以让每本书记住每章的位置 [行号]。

这是我编写的一个简单的解析器,它可以让您知道该去哪里。如果你不介意它只是一个终端解析器,那么你可以根据你的需要修改我的代码 - 如果你正在考虑将它变成一个 GUI 那么它超出了我的答案的范围,但请查看 tkinter 或 pygame 等库,具体取决于就看你想写多少。 (仍然使用这段代码作为后端来实际获取数据)。

确保圣经文件 KJV.txt 与您的脚本位于同一文件夹中。

class Book:
    def __init__(self, name, start_index, end_index, lines):
        self.name = name
        self.lines = lines[start_index:end_index+1]
        self.chapters = {}
        for index, line in enumerate(self.lines):
            numeric = line.split(" ")[1].split(":")[0]
            if self.chapters.get(numeric) == None:
                self.chapters[numeric] = index
                
    def get_passage(self, chapter, verse):
        index = self.chapters[str(chapter)] + int(verse) - 1
        return self.lines[index]
    
with open("KJV.txt", "r") as bible_file:
    lines = [line.strip("\n") for line in bible_file.readlines()][2:]

books = []
indices = []

book_objects = {}

for index, line in enumerate(lines):
    book = line.split(" ")[0]
    if book not in books:
        books.append(book)
        indices.append(index)

for index, book in enumerate(books):
    try: end = indices[index+1]-1
    except: end = len(lines)-1
    print("BOOK: %s AT %s" % (book, index))
    book_objects[book.lower()] = Book(book, indices[index], end, lines)

verse = book_objects["exodus"].get_passage(7, 11)
print(verse)
© www.soinside.com 2019 - 2024. All rights reserved.