话不多说直接上代码
import os
from PyQt5.QtWidgets import QWidget, QApplication, QComboBox, QPushButton
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
def setup_ui(self):
self.resize(500, 100)
self.setWindowTitle("更改pip源")
# 实例化QComBox对象
self.cb = QComboBox(self)
self.cb.resize(320, 30)
self.cb.move(90, 10)
# 添加条目
self.cb.addItems(["清华镜像源", "阿里巴巴镜像源", "豆瓣镜像源", "中国科技大学镜像源", "山东理工大学镜像源", "华中理工大学镜像源"])
# 信号
# print(self.cb.currentText())
self.cb.index = 0
# 返回选中项文本
# print(self.cb.currentIndex())
# 返回选中项索引
self.cb.currentIndexChanged.connect(self.print_v) # 条目发生改变,发射信号,传递条目索引
# 清华索引0,阿里索引1,豆瓣索引2,中国科技大学3,山东理工大学4, 华中理工大学5
# 添加按钮
self.submit = QPushButton(self)
self.submit.setText("修改")
self.submit.resize(320, 30)
self.submit.move(90, 50)
self.submit.clicked.connect(self.write_ini)
self.submit.show()
self.show()
def print_v(self, i):
print(self.cb.currentText())
# print(i)
self.cb.index = i
def write_ini(self):
# print(self.cb.currentIndex())
# 返回选中项索引
index = self.cb.index
if index == 0:
source = "https://pypi.tuna.tsinghua.edu.cn/simple"
source_trusted = "pypi.tuna.tsinghua.edu.cn"
# 清华镜像源
elif index == 1:
source = "http://mirrors.aliyun.com/pypi/simple/"
source_trusted = "mirrors.aliyun.com"
# 阿里镜像源
elif index == 2:
source = "http://pypi.douban.com/simple/"
source_trusted = "pypi.douban.com"
# 豆瓣镜像源
elif index == 3:
source = "http://pypi.mirrors.ustc.edu.cn/simple/"
source_trusted = "pypi.mirrors.ustc.edu.cn"
# 中国科学技术大学镜像源
elif index == 4:
source = "http://pypi.sdutlinux.org/simple/"
source_trusted = "pypi.sdutlinux.org"
# 山东理工大学镜像源
elif index == 5:
source = "http://pypi.hustunique.com/simple/"
source_trusted = "pypi.hustunique.com"
# 华中理工大学镜像源
config = "[global]\nindex-url = " + source + "\n[install]\ntrusted-host=" + source_trusted
pip_path = os.environ["USERPROFILE"] + "\\pip\\"
if not os.path.exists(pip_path):
os.makedirs(pip_path)
with open(pip_path + "pip.ini", "w+") as f:
f.write(config)
if __name__ == "__main__":
app = QApplication(sys.argv)
win_root = Window()
sys.exit(app.exec_())