我在GUI中使用QtDesigner(在eric4)設計我剛做了一個Qlabel作爲佔位符,用於更新最新的ID可以訪問我正在構建的SQLite數據庫。我希望它setlabel改變到在ID主鍵可用的下一個時隙,所以我由在單獨的py文件中的類和DEF:對話窗口中的標籤不會更新每個版本的點擊按鈕主窗口(PyQt)
class AccessMem:
def LastRowID(self):
con = sqlite3.connect("Members.db") #access database
cur = con.cursor() #cursor object for database
#created if first time to make a table
try:
cur.execute('create table Members (ID integer primary key autoincrement not null, Nick string not null, NameFirst string, NameMiddle string, NameLast string, BirthMonth string, BirthDay string, BirthYear string, ContactNum string, EMail string, MailAdd string, MemberSince string)')
except:
pass
#only to get the value of NextID to display
TempNick = "ThisIsADummyNickToBeDeleted"
cur.execute("insert into Members (Nick) values (?)", (TempNick,))
NextID = cur.lastrowid
cur.execute("delete from Members where ID = ?", (NextID,))
return NextID
cur.close()
con.close()
我然後修改的編譯UI形式和加入這些在頂部(其中AccessMem功能的單獨的PY文件memberinfo.py):
from memberinfo import *
IDL = AccessMem()
IDLabel = `IDL.LastRowID()`
然後,我已經改變了def retranslateUi
部分,其中顯示必須改成這樣:
self.ID_Number.setText(IDLabel)
,以便它在對話窗口打開時顯示。
問題是,無論何時運行程序並在主窗口中啓動該對話框窗口,ID_Number的顯示都不會在對話框窗口中更新。每當我關閉程序時,再次運行它,然後再次在主窗口上單擊以啓動對話框窗口,將ID_Number顯示更新。
以防萬一,這裏是在主窗口中,我加入到啓動對話窗口的對話框代碼的部分:在上面我說:
from ui.newmember import NewMember
然後在使用addMember點擊事件:
def on_AddMember_clicked(self):
"""
Slot documentation goes here.
"""
# Open New Member window
self.NM = NewMember()
self.NM.show()
NewMember是ui文件夾中newmember.py中類的名稱。
請注意,在NewMember對話窗口中,它基本上爲sqlite數據庫添加了一組新的數據。以下是該事件的對話編碼的部分如果單擊保存按鈕:
def on_button_Save_released(self):
"""
Slot documentation goes here.
"""
Nik = unicode(self.LineEdit_Nickname.text())
NFirst = unicode(self.LineEdit_NameFirst.text())
NMid = unicode(self.LineEdit_NameMiddle.text())
NLast = unicode(self.LineEdit_NameLast.text())
BMon = unicode(self.ComboBox_BirthMonth.currentText())
BDay = unicode(self.ComboBox_BirthDay.currentText())
BYear = unicode(self.ComboBox_BirthYear.currentText())
CNum = unicode(self.LineEdit_ContactNum.text())
EM = unicode(self.LineEdit_EMail.text())
MAd = unicode(self.LineEdit_MailAdd.text())
self.NMem = NewMem()
self.NMem.input_data(Nik, NFirst, NMid, NLast, BMon, BDay, BYear, CNum, EM, MAd)
self.close()
的input_data方法是在一個單獨的PY文件是這樣的:
class NewMem:
def input_data(self, Nik, NFirst, NMid, NLast, BMon, BDay, BYear, CNum, EM, MAd):
con = sqlite3.connect("Members.db") #access database
cur = con.cursor() #cursor object for database
def adapt_datetime(ts):
return time.mktime(ts.timetuple())
#Get current time and date
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
now = datetime.datetime.now()
#created if first time to make a table
try:
cur.execute('create table Members (ID integer primary key autoincrement not null, Nick string not null, NameFirst string, NameMiddle string, NameLast string, BirthMonth string, BirthDay string, BirthYear string, ContactNum string, EMail string, MailAdd string, MemberSince string)')
except:
pass
cur.execute("insert into Members (Nick,NameFirst,NameMiddle,NameLast,BirthMonth,BirthDay,BirthYear,ContactNum,EMail,MailAdd,MemberSince) values (?,?,?,?,?,?,?,?,?,?,?)",(Nik, NFirst, NMid, NLast, BMon, BDay, BYear, CNum, EM, MAd, now))
con.commit()
cur.close()
con.close()
所以當NewMember對話窗口的單擊「保存」按鈕,關閉窗口,sqlite插入新的數據集。然後在主窗口中的按鈕啓動NewMember窗口,IDLabel必須更新爲最新的行ID,但它仍然沒有自行更新。
有沒有可能問如何解決這個問題?任何幫助,非常感謝深深的蝴蝶結