Kivy - 在MongoDB中添加数据时,小部件不会刷新

时间:2016-02-25 12:27:24

标签: python mongodb kivy

所以这就是我正在做的事情。我生成按钮取决于我的数据库中有多少数据,我有一个'上传'按钮,用于在数据库中添加数据。添加数据时,应该有另一个必须生成的按钮。

生成按钮的屏幕和“上传”的屏幕。数据不同。

当我在上传屏幕上成功添加数据时,我所做的是从OtherVideos类调用loadVideos方法,如下所示:OtherVideos()。loadVideos

所以这是我的代码:

class OtherVideos(BoxLayout):

main = ObjectProperty()

def __init__(self, *args, **kwargs):
    super(OtherVideos,self).__init__(*args, **kwargs)
    self.loadVideos()

def loadVideos(self):
    print("loadingvids")
    self.clear_widgets()
    con = MongoClient()
    db = con.nba
    vids = db.videos.find()

    vidnum = 1
    for filename in vids:
        myid = "vid" + str(vidnum)
        getfilename = filename['filename']

        button = Button(id=myid,
                      text=getfilename,
                      color=[0,0.7,1,1],
                      background_color=[1,0.1,0.1,1],
                      bold=1)
        button.bind(on_release=partial(self.change_Title, getfilename))
        button.bind(on_release=partial(self.change_Vid, getfilename))
        self.add_widget(button)
        vidnum += 1

def change_Title(self, title, *args):
    self.main.change_curr_title(title)

def change_Vid(self, myfilename, *args):
    con = MongoClient()
    db = con.nba
    vids = db.videos.find()

    for filename in vids:
        file = os.path.basename(filename['video_path'])
        if myfilename == filename['filename']:
            MainApp.vid = filename['video_path']
            break

    self.main.change_curr_vid(filename['video_path'])
pass

class UploadScreen(Screen):
file_path = ''
file_name = ''

def browseFile(self):
    root = tk.Tk()
    root.withdraw()
    self.file_path = askopenfilename(filetypes=(("Video files", "*.mp4;*.flv;*.avi;*.mkv"),
                                       ("All files", "*.*") ))
    file = os.path.basename(self.file_path)

    self.ids.filePath.text = self.file_path
    self.file_name = os.path.splitext(file)[0]

def uploadFile(self):
    if os.path.exists(self.ids.filePath.text):
        con = MongoClient()
        db = con.nba
        videos = db.videos

        videos.insert(
            {
                'video_path': self.ids.filePath.text,
                'filename': self.file_name
            })
        popup = Popup(title='NBA Highlights',
                      content=Label(text='Video has been uploaded successfully.'),
                      size_hint=(None, None), size=(400, 300))
        popup.open()
        self.ids.filePath.text = ''
        OtherVideos().loadVideos #<-------- This is not working. There's no error but it doesn't refresh the widgets.
    else:
        popup = Popup(title='NBA Highlights',
                      content=Label(text='Invalid path, make sure your video path is correct.'),
                      size_hint=(None, None), size=(400, 300))
        popup.open()

pass
在kivy文件中:

<MainApp>:
....
BoxLayout:
....
    BoxLayout:
    ....some widgets
    BoxLayout:
        OtherVideos:
            ...this is where the buttons are generated...
        BoxLayout:

<UploadScreen>:
    BoxLayout:
        ...
        BoxLayout:
             label
             text input
             browse button
             upload button
        BoxLayout:
             back button

整个代码在这里:http://textuploader.com/52v1q

希望你们能帮助我!

0 个答案:

没有答案