python - 使用 Tkinter 的“pip install Module”程序

标签 python tkinter error-handling pip progress-bar

如何在程序中显示控制台错误信息?
我用 tkinter 制作了一个程序,它通过简单地编写模块的名称来安装带有 Pip 的 python 模块。
但问题是我只能在有人不写任何东西时显示错误消息,例如,当模块已安装或不存在时。我想要这些错误消息,以便每次在普通控制台上出现错误时,它都会出现在我的程序中。

from tkinter import *
import os

window = Tk()
window.geometry('500x360')
window.title('Pip Module Installer')
window.configure(bg='Light Grey')
window.columnconfigure(0, weight=1)
# TITLE: PIP MODULE INSTALLER
welcome = Label(window, text='Pip Module Installer', fg='Dark Blue', bg='Light Grey', font=('Calibri', 15))
welcome.grid(row=0, column=0)
# TEXT: INTRODUCTION
introduction = Label(window,
                     text='This program allows you to install any Pip Module in an easy way!\n',
                     fg='Black', bg='Light Grey', font=('Calibri', 13))
introduction.grid(row=1, column=0, padx=20)

# FRAME: INSTRUCTIONS
group = LabelFrame(window, bg='Light Grey', bd=0,
                   highlightcolor='Dark Blue', highlightthickness=1.25)
group.grid(row=2, column=0, sticky='WE', padx=15, pady=10)
# TITLE: INSTRUCTIONS
label_title = Label(group, text='Instructions',
                    fg='Dark Blue', bg='Light Grey', font=('Calibri', 14))
label_title.grid(row=2, column=0, sticky='W', padx=10)
# TEXT: INSTRUCTIONS
instructions = Label(group, text='1. Type the name of the module, below:',
                     bg='Light Grey', font=('Calibri', 13))
instructions.grid(row=3, column=0, padx=10, pady=(0, 10))


def module_installer():
    if module_input.get():
        user_input = module_input.get()
        os.system('pip install ' + user_input)
        response = 'Installation done'
    else:
        response = 'ERROR: Please type the name of the module'

    response_label = Label(window, text='', bg='Light Grey', font=('Calibri', 13))
    response_label.grid(row=7, column=0, sticky='WE', padx=15, pady=(6, 0))
    response_label.config(text=response)
    module_input.delete(0, END)


# INPUT
module_input = Entry(group)
module_input.focus()
module_input.grid(row=4, column=0, sticky='WE', padx=25)
# TEXT: INSTRUCTIONS
instructions = Label(group, text='2. Click ',
                     bg='Light Grey', font=('Calibri', 13))
instructions.grid(row=5, column=0, sticky='W', padx=10, pady=(30, 15))
# INSTALL BUTTON
install_button = Button(group, text='Install', font=('Calibri', 13), command=module_installer)
install_button.grid(row=5, column=0, sticky='W', padx=(69, 0), pady=(30, 15))
# TEXT: INSTRUCTIONS
instructions = Label(group,
                     text='3. The program will do the rest',
                     bg='Light Grey', font=('Calibri', 13))
instructions.grid(row=6, column=0, sticky='NW', padx=10, pady=(0, 8))

#
if __name__ == '__main__':
    window.mainloop()
另外,如果您还有其他需要改进的地方,请提前感谢您!这是我的第一个 Tkinter GUI 程序!

最佳答案

您可以使用 subprocess.Popen()而不是 os.system并使用 Text小部件以显示控制台输出。
首先创建Text框下方group (LabelFrame):

# output log
output_log = Text(window, width=60, height=10, state='disabled')
output_log.grid(row=3, column=0, pady=(0,10))
然后更新module_installer()功能如下:
import sys
import subprocess as subp

...

def module_installer():
    user_input = module_input.get().strip()
    if user_input:
        # better use the same Python that runs this script, so use sys.executable
        proc = subp.Popen([sys.executable, '-m', 'pip', 'install', user_input], stdout=subp.PIPE, stderr=subp.PIPE)
        # get the console output and errors
        stdout, stderr = proc.communicate()
        if proc.returncode == 0:
            # pip returns 0
            response = stdout.decode()
        else:
            # there are errors
            response = stderr.decode()
    else:
        response = 'ERROR: Please type the name of the module'

    # show the response in the output box
    output_log['state'] = 'normal'
    output_log.replace('1.0', 'end', response)
    output_log['state'] = 'disabled'
    # clear user input
    module_input.delete(0, END)
请注意,您需要删除 window.geometry('500x360')或调整窗口大小以显示所有 Text盒子。

关于python - 使用 Tkinter 的“pip install Module”程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64518704/

相关文章:

c# - 捕获所有错误,而不仅仅是单个按钮

error-handling - 抑制整个脚本的错误

java - 需要帮助修复我的代码中的异常错误

python - Psychopy中的图像渲染问题

python - 单选按钮初始化,未选择任何选项

python - mayavi 在表面上映射一个离散的颜色条

python - 如何在 Python Tkinter 中将单选按钮的文本标签移动到按钮下方?

python - 使用 lxml 解析 HTML 中的段落

python - Tkinter 条目验证 -- 如何使用 %W

python - 使用 python 3 和 tkinter 中的 colorchooser 更改 tkinter 窗口的背景颜色