首页 > 代码库 > pyqt练习x1
pyqt练习x1
如图:
---------------
源代码1:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file ‘F:\untitled\user.ui‘
#
# Created: Tue May 06 18:02:26 2014
# by: PyQt4 UI code generator 4.9.6
#
# 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_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(489, 503)
self.tw = QtGui.QTableWidget(Form)
self.tw.setGeometry(QtCore.QRect(0, 0, 491, 470))
self.tw.setObjectName(_fromUtf8("tw"))
self.tw.setColumnCount(2)
self.tw.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.tw.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tw.setHorizontalHeaderItem(1, item)
self.stop1 = QtGui.QPushButton(Form)
self.stop1.setGeometry(QtCore.QRect(290, 437, 75, 23))
self.stop1.setObjectName(_fromUtf8("stop1"))
self.new_2 = QtGui.QPushButton(Form)
self.new_2.setGeometry(QtCore.QRect(390, 437, 81, 23))
self.new_2.setObjectName(_fromUtf8("new_2"))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(10, 479, 61, 16))
self.label.setObjectName(_fromUtf8("label"))
self.cpus = QtGui.QLabel(Form)
self.cpus.setGeometry(QtCore.QRect(70, 480, 54, 12))
self.cpus.setText(_fromUtf8(""))
self.cpus.setObjectName(_fromUtf8("cpus"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(140, 480, 54, 12))
self.label_2.setObjectName(_fromUtf8("label_2"))
self._subprocess = QtGui.QLabel(Form)
self._subprocess.setGeometry(QtCore.QRect(210, 480, 54, 12))
self._subprocess.setText(_fromUtf8(""))
self._subprocess.setObjectName(_fromUtf8("_subprocess"))
self.twidget = QtGui.QTableWidget(Form)
self.twidget.setGeometry(QtCore.QRect(0, 25, 491, 411))
self.twidget.setObjectName(_fromUtf8("twidget"))
self.twidget.setColumnCount(2)
self.twidget.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.twidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.twidget.setHorizontalHeaderItem(1, item)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
item = self.tw.horizontalHeaderItem(0)
item.setText(_translate("Form", "应用程序", None))
item = self.tw.horizontalHeaderItem(1)
item.setText(_translate("Form", "进程", None))
self.stop1.setText(_translate("Form", "结束任务(E)", None))
self.new_2.setText(_translate("Form", "新任务(N)...", None))
self.label.setText(_translate("Form", "cpu占用率:", None))
self.label_2.setText(_translate("Form", "进程个数:", None))
item = self.twidget.horizontalHeaderItem(0)
item.setText(_translate("Form", "当前任务", None))
item = self.twidget.horizontalHeaderItem(1)
item.setText(_translate("Form", "状态值", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
-----
源代码2
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file ‘F:\untitled\user1.ui‘
#
# Created: Sun May 04 14:00:35 2014
# by: PyQt4 UI code generator 4.9.6
#
# 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_Form1(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(448, 205)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(80, 30, 351, 20))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(10, 80, 54, 12))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.editfind = QtGui.QLineEdit(Form)
self.editfind.setGeometry(QtCore.QRect(70, 80, 361, 20))
self.editfind.setObjectName(_fromUtf8("editfind"))
self.bufind = QtGui.QPushButton(Form)
self.bufind.setGeometry(QtCore.QRect(411, 80, 21, 23))
self.bufind.setText(_fromUtf8(""))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("ims/q1.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.bufind.setIcon(icon)
self.bufind.setFlat(True)
self.bufind.setObjectName(_fromUtf8("bufind"))
self.label_3 = QtGui.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(10, 130, 421, 61))
self.label_3.setText(_fromUtf8(""))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.openfile03 = QtGui.QPushButton(Form)
self.openfile03.setGeometry(QtCore.QRect(300, 150, 75, 23))
self.openfile03.setObjectName(_fromUtf8("openfile03"))
self.openfile01 = QtGui.QPushButton(Form)
self.openfile01.setGeometry(QtCore.QRect(90, 150, 75, 23))
self.openfile01.setAutoExclusive(False)
self.openfile01.setAutoDefault(False)
self.openfile01.setFlat(False)
self.openfile01.setObjectName(_fromUtf8("openfile01"))
self.openfile02 = QtGui.QPushButton(Form)
self.openfile02.setGeometry(QtCore.QRect(190, 150, 75, 23))
self.openfile02.setObjectName(_fromUtf8("openfile02"))
self.ico1 = QtGui.QLabel(Form)
self.ico1.setGeometry(QtCore.QRect(18, 24, 54, 31))
self.ico1.setText(_fromUtf8(""))
self.ico1.setObjectName(_fromUtf8("ico1"))
self.CommBox1 = QtGui.QComboBox(Form)
self.CommBox1.setGeometry(QtCore.QRect(360, 100, 69, 22))
self.CommBox1.setFrame(True)
self.CommBox1.setObjectName(_fromUtf8("CommBox1"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "根据对应,请输入名称:为你打开相应的程序,文件夹或者url资源", None))
self.label_2.setText(_translate("Form", "输入内容:", None))
self.openfile03.setText(_translate("Form", "浏览(B)...", None))
self.openfile01.setText(_translate("Form", "打开", None))
self.openfile02.setText(_translate("Form", "取消", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form1()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
--------
源代码3
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file ‘F:\untitled\user3.ui‘
#
# Created: Sun May 04 18:51:15 2014
# by: PyQt4 UI code generator 4.9.6
#
# 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_Form2(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(20, 10, 91, 16))
self.label.setObjectName(_fromUtf8("label"))
self.tb = QtGui.QTextEdit(Form)
self.tb.setGeometry(QtCore.QRect(20, 30, 361, 251))
self.tb.setReadOnly(True)
self.tb.setObjectName(_fromUtf8("tb"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "文件内容显示区", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form2()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
------
源代码4
# -*- coding: utf-8 -*-
__author__ = ‘Administrator‘
from us import Ui_Form
from us1 import Ui_Form1
from user3 import Ui_Form2
from PyQt4 import Qt,QtCore,QtGui,QtWebKit
import sys,os,Image,win32process
class TableWidget(QtGui.QWidget):
def __init__(self,parent=None):
super(TableWidget,self).__init__(parent)
self.tables=Ui_Form()
self.tables.setupUi(self)
self.setWindowTitle(u‘简单的小工具‘)
self.tables.tw.horizontalHeader().setStretchLastSection(1)#设置充满表宽度
self.tables.tw.verticalHeader().setDefaultSectionSize(10)
self.tables.tw.horizontalHeader().resizeSection(0,220)#设置第一个宽度
self.tables.twidget.setColumnWidth(0,150)#设置当前宽度
self.tables.twidget.setColumnWidth(1,100)
self.tables.twidget.setToolTip(u‘进程显示‘)
self.tables.new_2.setDefault(1)
self.tables.stop1.setEnabled(0)
self.setFixedSize(490,500)
self.tables.new_2.setShortcut(‘Ctrl+N‘)
self.t2=T1()
self.t3=TN()
#self.tables.tw.horizontalHeader().setStyleSheet("QHeaderView::section{background:skyblue;}")#设置表头背景色
self.tables.new_2.clicked.connect(self.opensub)
self.t2.t1.openfile01.clicked.connect(self.OpenFile)#修改中
self.tables.twidget.itemClicked.connect(self.stop1s)
self.t2.t1.openfile01.clicked.connect(self.openssub3)
self.tables.stop1.clicked.connect(self.Roots)
self.tables.twidget.setRowCount(20)
self.tables.twidget.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
#self.tables.twidget.setEditTriggers(QtGui.QAbstractItemView.ExtendedSelection)
def opensubxx(self):
self.t3.show()
def opensub(self):
self.t2.show()
def OpenFile(self):#修改中
root2=QtGui.QTableWidgetItem(unicode(self.t2.t1.editfind.text()))
self.tables.twidget.setItem(0,0,root2)
#self.t2.close()
def openssub3(self):
if os.path.exists(unicode(self.t2.t1.editfind.text())) or(QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.t2.t1.editfind.text()))):
self.t2.t1.editfind.setText(unicode(self.t2.t1.editfind.text()))
QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(unicode(self.t2.t1.editfind.text())))
self.t2.close()
self.t2.t1.editfind.setText(unicode(self.t2.t1.editfind.text()))
else:
connects=QtGui.QMessageBox(self)
connects.setWindowTitle(u‘%s‘%unicode(self.t2.t1.editfind.text()))
lock1=connects.addButton(u‘确定‘,QtGui.QMessageBox.ActionRole)
connects.setText(u‘查找不到当前%s进程,请检查后重试‘%(unicode(self.t2.t1.editfind.text())))
connects.exec_()
button1=connects.clickedButton()
if button1==lock1:
self.t2.t1.editfind.clear()
self.tables.twidget.clear()
def Roots(self,a):
import win32api,subprocess,ctypes
item=self.tables.twidget.currentColumn()
self.tables.twidget.takeTopLevelItem(item)
process=subprocess.Popen([unicode(self.tables.twidget.currentItem()),‘-c‘,‘while 1:pass‘])
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)
#win32process.TerminateProcess(self.handle[0],0)
def Roots1(self):
import win32api,subprocess,ctypes
process=subprocess.Popen([self.tables.twidget.currentItem(),‘-c‘,‘while 1:pass‘])
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)
# self.handle = win32process.CreateProcess(unicode(self.tables.twidget.currentItem()),
# ‘‘, None , None , 0 ,win32process. CREATE_NO_WINDOW , None , None ,
# win32process.STARTUPINFO())
def stop1s(self):
self.tables.stop1.setEnabled(1)
if self.tables.twidget.columnCount()==2:
print ‘ok‘
class T1(QtGui.QDialog):
def __init__(self):
super(T1,self).__init__()
self.t1=Ui_Form1()
self.t1.setupUi(self)
self.t1.openfile01.setEnabled(0)
self.t1.openfile02.setEnabled(0)
self.t1.openfile03.setShortcut(‘Ctrl+B‘)
#self.t1.openfile03.setDefault(1)
self.t1.openfile03.clicked.connect(self.opensub1)
self.t1.openfile02.clicked.connect(self.Cancel)
self.t1.editfind.textChanged.connect(self.opensub2)
self.t1.CommBox1.setVisible(0)
self.t1.bufind.clicked.connect(self.CommBox)
self.t1.CommBox1.activated.connect(self.CommBox2)
self.t1.CommBox1.setStyleSheet("QComboBox{border:cleanlooks;}"
"QComboBox::drop-down{border:0px;}")
def opensub2(self):
if self.t1.editfind.text()==‘‘:
self.t1.openfile01.setEnabled(0)
self.t1.openfile02.setEnabled(0)
else:
self.t1.openfile01.setEnabled(1)
self.t1.openfile02.setEnabled(1)
def opensub1(self):
file11=QtGui.QFileDialog.getOpenFileName(self,‘Open file‘,‘/‘,u‘所有程序(*.*)‘)
self.t1.editfind.setText(unicode(file11))
if self.t1.editfind.text()==‘‘:
QtGui.QMessageBox.information(self,‘Error‘,‘Error‘)
else:
self.t1.openfile01.setEnabled(1)
self.t1.openfile02.setEnabled(1)
def Cancel(self):
self.close()
def CommBox(self,n):
self.t1.CommBox1.setVisible(1)
for i in range(10):
self.t1.CommBox1.addItems(unicode(i))
def CommBox2(self,text):
self.t1.editfind.setText(unicode(text))
#self.t1.editfind.adjustSize()
class TN(QtGui.QDialog):
def __init__(self):
super(TN,self).__init__()
self.tnb=Ui_Form2()
self.tnb.setupUi(self)
app=QtGui.QApplication(sys.argv)
table1=TableWidget()
table1.show()
sys.exit(app.exec_())
按照上面的图片,代码顺序和上面的图片一样位置
pyqt练习x1