题目1

描述:

PyQt5 完成一个简单的计算器

代码:

import sys,os
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
def openc():
    os.system('calc.exe')
    return
def calc_name():
    name = nameLine.text()
    name = name.replace('(','(')
    name = name.replace(')',')')
    name = name.replace('PI','3.1415926')
    name = name.replace('pai','3.1415926')
    name = name.replace('π','3.1415926')
    name = name.replace('pi','3.1415926')
    name = name.replace('e','2.71828')
    name = name.replace('E','*10**')
    name = name.replace('^','**')
    if name == '':
        QMessageBox.information(nameLine,'blank inputing','please input')
        return
    else:
        b = eval(name)
        QMessageBox.information(nameLine,'Done!','result:  {:.2f}'.format(b))

app = QApplication(sys.argv)
Enter = QWidget()
nameLabel = QLabel('calculate:')
nameLine = QLineEdit(Enter)
EnterButton = QPushButton('Enter',Enter)
CalcButton = QPushButton('Calc',Enter)
subLayout =QHBoxLayout()
subLayout.addWidget(CalcButton)
subLayout.addStretch(1)
subLayout.addWidget(EnterButton)
bodyLayout = QVBoxLayout()
bodyLayout.addWidget(nameLabel)
bodyLayout.addWidget(nameLine)
bodyLayout.addLayout(subLayout)
EnterButton.clicked.connect(calc_name)
CalcButton.clicked.connect(openc)
Enter.setLayout(bodyLayout)
Enter.setGeometry(300,200,400,100)
Enter.setWindowTitle('calculation')
Enter.show()
sys.exit(app.exec_())

  

 

题目2

描述:网页的保存

代码:

#!user/bin/python 3.5.2
#-*- encoding=utf-8   -*-
import requests
import os

url=input('please input a url:')
print ('hold on several seconds')
if url[0:4]!="http":
    url='http://'+url
kv={'user-agent':'Mozilla/5.0'}
r=requests.get(url,headers =kv)
r.raise_for_status()
r.encoding=r.apparent_encoding
ans=input('save the webpage?(y or n)')
if ans=='y':
    root=input('please input a path to save the webpage:')
    path=root+url.split('/')[-1]+'.html'
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        with open(path,'wb') as f:
            f.write(r.content)
            f.close()
            print('success!')
    else:
        print('the file has existed')