替换Tkinter小部件的内容

时间:2018-12-26 21:04:00

标签: python tkinter ttk

我正在尝试开发类似Twitter的客户端,以掌握tkinter的功能。我从sqlite数据库中存储的推文显示每个屏幕7条推文。一行在标签中包含名称,屏幕名称和创建日期。在其下是带有推文的文本框。当用户按下“下一个”或“上一个”按钮时,我要显示文件中的下一个或上一个7条推文。不确定如何清除先前的数据并更新新数据。那是因为我希望小部件的创建是动态的,如下面的代码所示。因此,无法解决特定小部件的名称。

 if tweetStr:
    while i <  listLen:   
        print(i)
        user_name = tweetStr[i]['name']
        screen_name = tweetStr[i]['screen_name']
        created = tweetStr[i]['created_at']
        tweet_text = (tweetStr[i]['short_text'] if not tweetStr[i]['long_text'] else tweetStr[i]['long_text'] )

        top_space = tk.Text(f, width= 100, height=1)
        top_space.grid(column = 0, row = start_row,columnspan=6, sticky="nsew")
        top_space.configure(background='lightblue')

        ttk.Label(f, text=user_name).grid(column=0, row=start_row+1,sticky='w')
        ttk.Label(f, text=screen_name).grid(column=1, row=start_row+1,sticky='w')
        ttk.Label(f, text=created).grid(column=2, row=start_row+1,sticky='w')

        t = tk.Text(f, width= 100, height=3, wrap='word')
        t.grid(column = 0, row = start_row+2,columnspan=6, sticky="nsew")
        #t.insert(INSERT, "Begin " + "This is a test " * 800 + " END" + '\n\n')
        t.insert(tk.INSERT, tweet_text)

        i += 1
        start_row += 6

2 个答案:

答案 0 :(得分:0)

对于文本小部件,请使用delete方法删除现有文本,然后使用insert添加新信息。避免一直破坏和重新创建小部件。创建一次,然后使用Text.insert和Label.configure更新其内容。 我还要避免使用while来调用after循环,直到处理完所有内容为止。紧密的循环会阻止事件的处理,直到您完成整个循环为止,而使用after则通过事件处理来分解循环的每次迭代。

答案 1 :(得分:0)

解决方案非常简单。

private void startAppDownload() {


    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean isDownloading = true;
            int downloadStatus, totalBytesDownloaded, totalBytes;



            while (isDownloading) {

                DownloadManager.Query downloadQuery = new DownloadManager.Query();
                downloadQuery.setFilterById(downloadID);

                Cursor cursor = downloadManager.query(downloadQuery);
                cursor.moveToFirst();

                totalBytesDownloaded = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                );

                totalBytes = cursor.getInt(
                        cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                );

                downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

                if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
                    isDownloading = false;
                    break;;
                }

                final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        downloadProgressBar.setProgress(downloadProgress);
                    }
                });
            }

            cursor.close();
        }
    }).start();
}