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()

抽獎程式原始碼

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])