对话框窗口中的标签不会更新每次单击按钮主窗口 (PyQt)
我使用 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 表单并将它们添加到顶部(其中单独的 py 文件AccessMem 函数是 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,但它仍然没有更新本身。
能请教一下这个问题如何解决吗?非常感谢任何帮助深深鞠躬
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.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()
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我在休息和睡觉后终于找到了解决方案。
我只是在编译的 UI 表单中做了一些更改,这些
更改首先位于最顶层,现在我将其放在
def retranslateUi
的方法中,现在它可以工作了。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
were first at the topmost, I put it now inside the method of
def retranslateUi
and it now works.