我们可以在 python 中向终端/控制台添加可点击的文本或按钮吗?

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

例如这是我的功能:

def get_sum(item1, item2):
  print(int(item1) + int(item2))

但是我想在用户单击命令提示符或任何终端中的某个项目时触发该功能。
主要问题:-
是否可以通过点击终端来运行函数?

额外
我还在终端中看到了 VS-Code 扩展开发输出。当我运行

yo code
时,它显示出非常好的东西。用户可以使用箭头键选择内容并按 Enter 运行它。那么这在Python中也可能吗? enter image description here
谢谢!

python
4个回答
8
投票

编辑:你不能通过点击东西来触发函数在Python,但我认为你可以在C++中

我将使用curses模块,还有一个适用于Windows的移植版本,但我不确定这是否有效...

这段代码有点复杂

import curses

# define the menu function
def menu(title, classes, color='white'):
  # define the curses wrapper
  def character(stdscr,):
    attributes = {}
    # stuff i copied from the internet that i'll put in the right format later
    icol = {
      1:'red',
      2:'green',
      3:'yellow',
      4:'blue',
      5:'magenta',
      6:'cyan',
      7:'white'
    }
    # put the stuff in the right format
    col = {v: k for k, v in icol.items()}

    # declare the background color

    bc = curses.COLOR_BLACK

    # make the 'normal' format
    curses.init_pair(1, 7, bc)
    attributes['normal'] = curses.color_pair(1)


    # make the 'highlighted' format
    curses.init_pair(2, col[color], bc)
    attributes['highlighted'] = curses.color_pair(2)


    # handle the menu
    c = 0
    option = 0
    while c != 10:

        stdscr.erase() # clear the screen (you can erase this if you want)

        # add the title
        stdscr.addstr(f"{title}\n", curses.color_pair(1))

        # add the options
        for i in range(len(classes)):
            # handle the colors
            if i == option:
                attr = attributes['highlighted']
            else:
                attr = attributes['normal']
            
            # actually add the options

            stdscr.addstr(f'> ', attr)
            stdscr.addstr(f'{classes[i]}' + '\n', attr)
        c = stdscr.getch()

        # handle the arrow keys
        if c == curses.KEY_UP and option > 0:
            option -= 1
        elif c == curses.KEY_DOWN and option < len(classes) - 1:
            option += 1
    return option
  return curses.wrapper(character)

这是菜单功能,现在这是如何使用该功能的示例

print(f"output:", menu('TEST', ['this will return 0','this will return 1', 'this is just to show that you can do more options then just two'], 'blue'))

函数的语法如下

menu('title', ['options', 'optionssssssss'], 'color (optional)')

以下是可用的颜色:

红色
绿色
黄色
蓝色
洋红色
青色
白色

示例代码的输出如下所示:
gif

您可以使用以下命令安装 Windows 版本:

python -m pip install windows-curses

但我认为它甚至不适用于 python 3

如果这篇文章不好,我也很抱歉,这是我在 stackoverflow 上的第一个答案


2
投票

不知道按键,但我找到了答案:-
我们可以使用

keyboard
模块。

import keyboard 

abc = True

def run_func():
  print('Hi, welcome!')
  print('If you want to do this, press A or press B for doing that.')
  while abc:
    if keyboard.is_pressed('a'):
      print('This function is run!')
      abc = True

    elif keyboard.is_pressed('b'):
      print('That function is run!')
      abc = True

print('To start press S')
while abc:
  if keyboard.is_pressed('S'): 
    run_func()
  abc = True

使用:当我们按下按键时,我们可以运行函数和许多东西!


0
投票

我的列表=[5,2,3,4,1] 我的清单。种类() 打印(我的清单) 我的清单。排序(反向=true)


-2
投票

这可以帮助某人

from tkinter import colorchooser,Tk,messagebox
from colored import fg,bg,attr
import win32console
import threading
import win32gui
import win32api
import colorama
import atexit
import mouse
import time
import sys
import os

class console:
    def __init__(self):
        self._is_enabled = False
        self._last_pos = (0,0)
        self.hwnd = win32console.GetConsoleWindow()
        atexit.register(lambda:self.set_input(True))
    def _mouse_thread(self):
        while True:
            if self.get_mouse_position()[1] > 30:
                if self.is_input():
                    self._set_input(False)
            else:
                if not self.is_input():
                    self._set_input(True)
    def _focus_thread(self):
        while True:
            if win32gui.GetForegroundWindow() != self.hwnd:
                win32gui.SetForegroundWindow(self.hwnd)
    def run_focus_thread(self):
        threading.Thread(target=self._focus_thread,daemon=1).start()
    def run_moveable_thread(self):
        threading.Thread(target=self._mouse_thread,daemon=1).start()
    def get_position(self):
        return win32gui.GetWindowRect(self.hwnd)[:2]
    def get_mouse_position(self):
        try:
            pos = win32gui.GetCursorPos()
            self._last_pos = pos
        except:
            pos = self._last_pos
        return (pos[0]-self.get_position()[0]-7,
                pos[1]-self.get_position()[1]-30)
    def _set_input(self,value):
        win32gui.EnableWindow(self.hwnd,value)
    def set_input(self,value):
        self._is_enabled = value
        win32gui.EnableWindow(self.hwnd,value)
    def is_input(self):
        return win32gui.IsWindowEnabled(self.hwnd)
    def set_focus(self):
        win32gui.SetForegroundWindow(self.hwnd)
        win32gui.SetFocus(self.hwnd)
    def is_focus(self):
        return win32gui.GetForegroundWindow() == self.hwnd and \
               self.get_mouse_position()[1] >= 30 and \
               self.get_mouse_position()[0] > 0 and \
               self.get_mouse_position()[0] <= self.get_size()[0]-7 and \
               self.get_mouse_position()[1] <= self.get_size()[1]
    def get_size(self):
        return (win32gui.GetWindowRect(self.hwnd)[2]-7,
                win32gui.GetWindowRect(self.hwnd)[3]-30)
    def set_size(self,w,h):
        win32gui.MoveWindow(self.hwnd,*self.get_position(),int(w),int(h),True)
    def set_position(self,x,y):
        win32gui.MoveWindow(self.hwnd,int(x),int(y),*self.get_size(),True)
    def set_rect(self,x,y,w,h):
        win32gui.MoveWindow(self.hwnd,int(x),int(y),int(w),int(h),True)
    def set_center(self):
        self.set_position(get_screen_size()[0]/2-self.get_size()[0]/2,
                          get_screen_size()[1]/2-self.get_size()[1]/2)
    def set_title(self,title):
        win32gui.SetWindowText(self.hwnd,title)
console = console()

def get_screen_size():
    return (win32api.GetSystemMetrics(0),
            win32api.GetSystemMetrics(1))

font_size = [8.2,15.5]

class canvas:
    def __init__(self,width,height):
        self.sp = (width*font_size[0],height*font_size[1])
        self.width, self.height = width, height
        self.changed = True
        self.pixels = [[" " for i in range(self.width)] for i in range(self.height)]
        console.set_size(self.sp[0]+30,self.sp[1]+10)
    def clear(self):
        # os.system("cls")
        print("\033[H\033[J",end="")
    def fill(self,symbol=" ",update=True):
        self.pixels = [[symbol for i in range(self.width)] for i in range(self.height)]
        self.changed = True
        if update: self.update()
    def update(self):
        self.changed = False
        out = ""
        for y in self.pixels:
            for x in y:
                out += x
            out += "\n"
        self.clear()
        print(out,end="")
    def set(self,x,y,symbol,update=True):
        if x < self.width and y < self.height:
            self.pixels[y][x] = symbol
            self.changed = True
            if update: self.update()
    def write(self,x,y,text,update=True):
        self.changed = True
        for n,s in enumerate(text):
            self.set(x+n,y,s,False)
        if update: self.update()
    def get(self,x,y):
        return self.pixels[y][x]
    def all(self):
        return self.pixels
    def get_mouse_position(self):
        return (console.get_mouse_position()[0]/self.sp[0]*self.width,
                (console.get_mouse_position()[1]/(self.sp[1]-font_size[1]*4)*self.height))

def color_symbol(color):
    return fg(color)+bg(color)+"#"+attr('reset')


# -------   create canvas   -------

console.set_title("hello world button")  # set console title
console.run_moveable_thread()  # run thread that prohibits clicking on the console
win = canvas(64,34)            # creating canvas


# -------   create buttons   -------

btns = []
def btn(text,x,y,cb):
    btns.append([text,x,y,cb,False])

def btncallback():
    Tk().withdraw()
    messagebox.showinfo("hello world","Hello World!")

btn("click me!",25,15,btncallback)


# -------   create main view   -------

for i in btns:
    win.set(i[1],i[2],i[0][0],False)
    win.write(i[1]+1,i[2],i[0][1:][:-1],False)
    win.set(i[1]+len(i[0])-1,i[2],i[0][-1],False)
win.update()


# -------   main loop   -------


run = True
while run:
    if win.changed:
        win.update()
    if console.is_focus():
        pos = win.get_mouse_position()
        pos = (round(pos[0]),round(pos[1]))
        if mouse.is_pressed("left"):
            for i in btns:
                if pos[0] >= i[1] and pos[0] <= i[1]+len(i[0]) \
                            and pos[1] == i[2]+1 and not i[4]:
                    win.set(i[1],i[2],fg("black")+bg("white")+i[0][0],False)
                    win.write(i[1]+1,i[2],i[0][1:][:-1],False)
                    win.set(i[1]+len(i[0])-1,i[2],i[0][-1]+attr("reset"),False)
                    i[4] = True
        else:
            for i in btns:
                if i[4]:
                    win.set(i[1],i[2],i[0][0],False)
                    win.write(i[1]+1,i[2],i[0][1:][:-1],False)
                    win.set(i[1]+len(i[0])-1,i[2],i[0][-1],False)
                    i[4] = False
                    i[3]()

© www.soinside.com 2019 - 2024. All rights reserved.