gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:03:54 35 4
gpt4 key购买 nike

如何在程序中显示控制台错误信息?
我用 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com