我在使用QtDesigner(在eric4)的GUI中进行了设计我刚刚制作了一个Qlabel作为占位符,旨在使用可以访问我正在构建的SQLite数据库的最新ID进行更新。我希望它setlabel更改为ID主键中可用的下一个插槽,所以我在一个单独的py文件中创建了一个类和def:
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 Dialog Window的“Save”按钮时,Window将关闭,sqlite已插入新的数据集。然后在主窗口中的按钮启动NewMember窗口,IDLabel必须更新为最新的行ID,但它仍然没有更新。
有可能问如何解决这个问题吗?任何帮助都非常感谢深深地鞠躬
答案 0 :(得分:0)
好的,我在休息和睡觉后最终找到了解决方案。
我刚刚在已编译的UI表单中进行了一些更改
IDL = AccessMem()
IDLabel = `IDL.LastRowID()`
首先位于最顶端,我现在将其放在def retranslateUi
的方法中,现在可以使用了。