Label in a dialog window does not update every release of click of button Main Window (PyQt)
I've designed in a GUI using QtDesigner (at eric4) I've just made a Qlabel as a placeholder which is intended to update with the latest ID that has access to a SQLite database which I'm building. I want it to setlabel to change to the next slot available in ID primary key so I made a class and def in a separate py file:
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()
I then modified the compiled UI form and added these at the top (where the separate py file of the AccessMem function is memberinfo.py):
from memberinfo import *
IDL = AccessMem()
IDLabel = `IDL.LastRowID()`
And then I've changed the def retranslateUi
part where the display must be changed to this:
self.ID_Number.setText(IDLabel)
so that it could display when the Dialog Window is going to open.
Problem is that whenever I run the program and in the Main Window, launching for that Dialog Window, the display of the ID_Number is not updated in the Dialog Window. Whenever I close the program, run it again, then clicked again on the Main Window to launch the Dialog Window will the ID_Number display update.
Just in case, here are parts of the Dialog Code in the Main Window which I added to launch the Dialog Window: At the top I added:
from ui.newmember import NewMember
Then at the AddMember clicked event:
def on_AddMember_clicked(self):
"""
Slot documentation goes here.
"""
# Open New Member window
self.NM = NewMember()
self.NM.show()
NewMember is the name of the class in the newmember.py in ui folder.
Just noting also that in the NewMember Dialog Window, it basically adds a new set of data for the sqlite database. Here are parts of the Dialog Code on the event if the Save button is clicked:
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.cur开发者_JS百科rentText())
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()
The input_data method is in a separate py file and goes like this:
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()
So when NewMember Dialog Window's "Save" button is clicked, the Window is closed and sqlite has inserted new set of data. Then the button in the Main Window to launch the NewMember window, the IDLabel must be updated to the latest row ID but it still didn't update itself.
Would it be possible to ask how to solve this? Any help is much appreciated bows deeply
Okay, I ended up finding the solution after taking some rest and sleeping.
I just did some changing in the compiled UI form where these
IDL = AccessMem()
IDLabel = `IDL.LastRowID()`
were first at the topmost, I put it now inside the method of def retranslateUi
and it now works.
精彩评论