ผมเขียน application ขึ้นมาด้วยหนึ่งใช้ python+qt ตรง process ที่รันจะมีการแสดงสถานะ progress bar โดยบวกค่าที่ละ 1 โดยผมจะใช้เป็น Loop For ในการทำงาน และระหว่างนั้นผมจะให้แสดงข้อความลง Label เพื่อแสดงให้รู้ว่าทำงานถึงนะจุดใด ตอนนี้มันแสดงค่าสุดท้าย ตอนจบการทำงานทีเดียวครับช่วยผมด้วยครับ
import thread import sys from qt import * from kdecore import KCmdLineArgs, KApplication from kdeui import * class Form(QDialog): def __init__(self,parent = None,name = None,modal = 0,fl = 0): QDialog.__init__(self,parent,name,modal,fl) if not name: self.setName("Form") self.lbl_show = KActiveLabel(self,"lbl_show") self.lbl_show.setGeometry(QRect(20,70,220,21)) self.lbl_password = KActiveLabel(self,"lbl_password") self.lbl_password.setGeometry(QRect(20,10,80,21)) self.password = KPasswordEdit(self,"password") self.password.setGeometry(QRect(100,10,141,21)) self.cmdOK = KPushButton(self,"cmdOK") self.cmdOK.setGeometry(QRect(80,110,91,31)) self.pb1 = KProgress(self,"pb1") self.pb1.setGeometry(QRect(20,40,221,23)) self.pb1.setTotalSteps(100) self.pb1.setProgress(1) self.languageChange() self.resize(QSize(244,152).expandedTo(self.minimumSizeHint())) self.clearWState(Qt.WState_Polished) self.connect(self.cmdOK,SIGNAL("pressed()"),self.newSlot) self.connect(self.pb1,SIGNAL("percentageChanged(int)"),self.newSlot2) def languageChange(self): self.setCaption(self.__tr("Form")) self.lbl_show.setText(self.__tr("Ready")) self.lbl_password.setText(self.__tr("Password:")) self.cmdOK.setText(self.__tr("OK")) def newSlot2(self): print "Start Thread" print "Thread No.",thread.get_ident() self.lbl_show.setText(self.__tr('Write:' +str(self.pb1.progress())+'%')) self.cmdOK.setText(self.__tr('Write:' +str(self.pb1.progress())+'%')) print "End Thread" def newSlot(self): for j in range(1,5): for i in range(1,101): self.pb1.setProgress(i) #thread.start_new_thread(self.newSlot2, ()) self.lbl_show.setText(self.__tr('Write ID :' +str(j)+'%')) self.cmdOK.setText(self.__tr('Write ID :' +str(j)+'%')) def __tr(self,s,c = None): return qApp.translate("Form",s,c) if __name__ == "__main__": appname = "" description = "" version = "" KCmdLineArgs.init (sys.argv, appname, description, version) a = KApplication () QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()")) w = Form() a.setMainWidget(w) w.show() a.exec_loop()
กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น
blockcode ช่วยทำให้ code อ่านง่ายขึ้นได้ครับ http://blog.vee-u.com/2008/02/17/blockcode/
ไม่ได้ใช้ thread นี่ มี comment ไว้
คร้บผมลองใช้thread แล้ว ไม่ได้เหมือนกันครับ ช่วยผมด้วยครับ
โปรแกรมมันทำงานตรงไหน ถ้าหมายถึง 3 บรรทัดนี้
ก็ควรใส่ใน
newSlot2แล้วก็น่าจะมี delay sleep ด้วย ไม่งั้นมันก็เร็วจี๊ด ไม่ทันเห็นอะไรผมแก้แล้วมันก็ยังไม่ได้เหมือนเดืมครับ
ดูเหมือนว่าจะเข้าใจ thread ผิดไปหน่อย ใน newSlot แค่เรียก start_new_thread() ก็พอครับ นอกนั้นควรอยู่ใน newSlot2
ได้แล้วครับขอบคุณ คุณ sugree มากๆ นะครับ
เก่งมาก เก่งมาก