受保護的文章:python_06_hw
受保護的文章:python_05_hw
url編碼範例
# -*- coding: utf-8 -*-
import urllib.request
urlstr=urllib.parse.quote('鋼鐵人')
url = 'http://tw.movie.yahoo.com/moviesearch_result.html?qmv='+urlstr
response = urllib.request.urlopen(url)
html = response.read().decode('utf_8')
print(html)
參考電子書
PyQT 按鈕事件範例
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sat Jun 8 21:02:00 2013
# by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(329, 414)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(30, 30, 251, 31))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(30, 100, 251, 31))
self.label.setObjectName(_fromUtf8("label"))
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(120, 250, 75, 41))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label_2 = QtGui.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(30, 160, 251, 31))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.label.setText)
QtCore.QMetaObject.connectSlotsByName(Dialog)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.btnTest)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
self.label.setText(_translate("Dialog", "TextLabel", None))
self.pushButton.setText(_translate("Dialog", "PushButton", None))
self.label_2.setText(_translate("Dialog", "TextLabel", None))
def btnTest(self):
self.label_2.setText(self.lineEdit.text())
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
sqlite 連線範例
# -*- coding: utf-8 -*-
import sqlite3
conn = sqlite3.connect('starbucks.sqlite')
cur=conn.cursor()
cur.execute("insert into 'stores' ('longitude','latitude','name','address','tel','wifi','notes') VALUES(20,100,'測試店家','台北市羅斯福路1段1號','0222223333','Y','')")
cur.execute('select * from stores')
conn.commit()
for store in cur.fetchall():
print(store)
#print(cur.fetchall()[99])
cur.close()
conn.close()
[GUI]PyQt4 helloworld範例
1.http://www.riverbankcomputing.co.uk/software/pyqt/download
2.pyuic4 -x helloworld.ui -o helloworld.py
Class類別課堂練習
s1 = student("Tom","M")
s2 = student("Jane","F")
s3 = student("John","M")
s4 = student("Ann","F")
s5 = student("Peter","M")
s1.add(80)
s1.add(90)
s1.add(55)
s1.add(77)
s1.add(40)
s2.add(58)
s2.add(87)
s3.add(100)
s3.add(80)
s4.add(40)
s4.add(55)
s5.add(60)
s5.add(60)
抽獎程式原始碼
import time
import random
file = open('225python.txt', 'r', encoding='UTF-8')
peopleList=[];
for line in file.readlines():
peopleList.append(line)
file.close()
seconds = list(i for i in range(1,11));
seconds.reverse()
print('抽獎開始 倒數10秒')
for i in list(seconds):
print(i)
time.sleep(1)
result=random.sample(peopleList,2);
print('恭喜以下兩位:n',result[0],result[1])