-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
97 lines (70 loc) · 2.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, \
QGridLayout, QTextEdit, QHBoxLayout, QVBoxLayout, QPushButton, \
QLabel,QMessageBox
import os
class App(QWidget):
hostsCode = ''
def __init__(self):
super().__init__()
hostsCode = os.popen('sudo cat /private/etc/hosts')
self.hostsCode = hostsCode.read()
self.initUI()
def initUI(self):
# self.resize(800, 600) # 可拉伸的窗口大小
self.setFixedSize(800, 400) # 固定窗口大小
self.center()
self.setWindowTitle('host修改工具【MAC版】')
# self.setStyleSheet("QLabel{background:#a9a9a9;padding-top:3px;padding-left:5px}")
Message1 = '本工具是基于python3+pyqt5制作,简化传统的vim /private/etc/hosts编辑文件,快速修改MAC系统下的host文件,听起来是不是美滋滋。'
label1 = QLabel(self)
label1.setText(Message1)
label1.setGeometry(10, 10, 780, 20)
Message2 = '作者:Nemo'
label2 = QLabel(self)
label2.setText(Message2)
label2.setGeometry(10, 30, 780, 20)
Message3 = '格式:127.0.0.1 localhost'
label3 = QLabel(self)
label3.setText(Message3)
label3.setGeometry(10, 50, 780, 20)
Message4 = '邮箱:[email protected]'
label4 = QLabel(self)
label4.setText(Message4)
label4.setGeometry(10, 70, 780, 20)
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
okButton.clicked.connect(self.savefile)
cancelButton.clicked.connect(self.cancelButton)
hbox = QHBoxLayout() # 水平布局
hbox.addStretch(0) # 增加了一些弹性空间
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QVBoxLayout() # 垂直布局
vbox.addStretch(1) # 增加了一些弹性空间
self.reviewEdit = QTextEdit()
self.reviewEdit.setText(self.hostsCode)
grid = QGridLayout()
grid.addWidget(self.reviewEdit, 9, 5, 1, 1)
vbox.addLayout(grid)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.show()
def savefile(self):
hostsCode = self.reviewEdit.toPlainText() #获取文本框内容
f = open('/private/etc/hosts', 'w') # 若是'wb'就表示写二进制文件
f.write(hostsCode)
f.close()
QMessageBox.about(self, "成功", "hosts文件修改成功")
def cancelButton(self):
self.close()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())