添加新条目时更新标签

时间:2019-04-17 17:34:14

标签: python python-3.x label kivy

每次添加新学生时,表中的数据都会更新,但是当我在同一会话中打开“显示学生详细信息”时,没有任何更改,但是当我关闭程序并再次运行时,更改是制作。如何在同一会话中更新标签。如您所见,我是新奇的猕猴桃,希望您能给个简单的解释

第一个是学生加法器,第二个是学生数据库

第一个是学生加法器,第二个是学生数据库

class add_student(Screen):

    def __init__(self,**kwargs):
        super(add_student,self).__init__(**kwargs)
        layout=BoxLayout(orientation="vertical",padding=10,spacing=10)
        self.lbl=Label(text="Enter student details",font_size=50)
        tf1=BoxLayout(padding=10)
        tf2=BoxLayout(padding=10)
        tf3=BoxLayout(padding=10)
        tf4=BoxLayout(padding=10)
        btns=BoxLayout(spacing=900,padding=10)
        stdnm=Label(text="Student Name:",font_size=30,size_hint_x=0.3)
        grd=Label(text="Grade:",font_size=30,size_hint_x=0.3)
        sec=Label(text="Sec:",font_size=30,size_hint_x=0.3)
        sid=Label(text="ID:",font_size=30,size_hint_x=0.33)
        self.sidt=TextInput(text="Please press Unique ID",font_size=40,multiline=False,input_filter="float")
        self.stdnmt=TextInput(font_size=40,multiline=False)
        self.grdt=TextInput(font_size=40,multiline=False,input_filter="float")
        self.sect=TextInput(font_size=40,multiline=False)
        self.sect.bind(on_text_validate=self.sbmt)
        rand=Button(text="Unique Id",size_hint=(.1,1),font_size=23)
        rand.bind(on_release=self.randomize)
        submit=Button(text="Submit",size_hint=(.1,.7),font_size=30)
        submit.bind(on_release=self.sbmt)
        back=Button(text="Back",size_hint=(.1,.7),font_size=30)
        back.bind(on_release=self.bck)
        tf1.add_widget(stdnm)
        tf1.add_widget(self.stdnmt)
        tf2.add_widget(grd)
        tf2.add_widget(self.grdt)
        tf3.add_widget(sec)
        tf3.add_widget(self.sect)
        tf4.add_widget(sid)
        tf4.add_widget(self.sidt)
        tf4.add_widget(rand)
        btns.add_widget(back)
        btns.add_widget(submit)
        layout.add_widget(self.lbl)
        layout.add_widget(tf4)
        layout.add_widget(tf1)
        layout.add_widget(tf2)
        layout.add_widget(tf3)
        layout.add_widget(btns)
        self.add_widget(layout)
    def bck(self,*args):
        self.manager.transition=FadeTransition()
        self.manager.current='student'
    def sbmt(self,*args):
        if self.sidt.text!="" and self.stdnmt.text!="" and self.grdt.text!="" and self.sect.text!="":
            sid=int(self.sidt.text)
            sname=str(self.stdnmt.text)
            grd=int(self.grdt.text)
            sec=str(self.sect.text)
            try:
                sql="INSERT INTO student(id,name,grade,sec) VALUES(%s,%s,%s,%s)"
                val=(sid,sname,grd,sec)
                crsr.execute(sql,val)
                popup = Popup(title='Success',content=Label(text=sname+' has been successfully added',font_size=26),
                              size_hint=(None, None), size=(500,150))
                popup.open()
                mydb.commit()
                self.sidt.text=""
                self.stdnmt.text=""
                self.grdt.text=""
                self.sect.text=""
            except:
                popup = Popup(title='Error',content=Label(text='Error adding student',font_size=25),
                              size_hint=(None, None), size=(450,150))
                popup.open()
        else:
            popup=Popup(title="Error",content=Label(text="Please enter all details",font_size=30),size_hint=(None,None),size=(450,150))
            popup.open()

    def randomize(self,*args):
        sql="SELECT id from student"
        crsr.execute(sql)
        myresult=crsr.fetchall()
        l=[]
        for i in myresult:
           l.append(i[0])
        while True:
           x=random.randint(100,999)
           if x not in l:
               self.sidt.text=str(x)
               break
           else:
               pass
class student_db(Screen):
    def __init__(self,**kwargs):
        super(student_db,self).__init__(**kwargs)
        blayout=BoxLayout(orientation="vertical")
        hdlayout=BoxLayout()
        glayout=GridLayout(cols=9,padding=50, spacing=50,size_hint=(None, None), width=1366)
        glayout.bind(minimum_height=glayout.setter('height'))
        glayout.clear_widgets()
        scroll = ScrollView(size_hint=(None, None), size=(1366,640),pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
        lbl=Label(text="All Student Details",font_size=40)
        back=Button(text="Back",size_hint=(.09,.6))
        back.bind(on_release=self.bck)
        crsr.execute("select* from student")
        std=crsr.fetchall()
        hd=["ID","Name","Grade","Sec","Borrowed","Date Borrowed","Return By","Book Condition","Fine"]
        for i in hd:
            head=Label(text=i,font_size=24)
            glayout.add_widget(head)
        crsr.execute("SELECT * FROM student")
        std=crsr.fetchall()
        for i in std:
            for j in i:
                det=Label(text=str(j),font_size=22)
                glayout.add_widget(det)
        hdlayout.add_widget(back)
        hdlayout.add_widget(lbl)
        scroll.add_widget(glayout)
        blayout.add_widget(hdlayout)
        blayout.add_widget(scroll)
        self.add_widget(blayout)
    def bck(self,*args):
        self.manager.transition=FadeTransition()
        self.manager.current='student'

0 个答案:

没有答案