1

QtDesigner (at eric4) を使用して GUI で設計しました。構築中の SQLite データベースにアクセスできる最新の ID で更新するためのプレースホルダーとして Qlabel を作成しました。ID 主キーで使用可能な次のスロットに変更するように setlabel したいので、別の py ファイルでクラスと定義を作成しました。

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 に更新する必要がありますが、それでも更新されませんでした。

これを解決する方法を尋ねることは可能でしょうか?どんな助けでも大歓迎です深くお辞儀をします

4

1 に答える 1

0

さて、私は少し休んで寝た後、解決策を見つけました。

コンパイルされた UI フォームでいくつかの変更を行いました。

IDL = AccessMem()
IDLabel = `IDL.LastRowID()`

最初は一番上にありましたが、今はメソッドの中に入れましたdef retranslateUi。今は動作します。

于 2011-03-02T02:57:05.883 に答える