Tkinter counter Label not showing up until function ends

Post here if you need help with creating a Graphical User Interface in Python.

Tkinter counter Label not showing up until function ends

Postby mckryall » Sat May 03, 2014 6:37 pm

I'm making a Tkinter (A.K.A. Module which I consistently pronounce "taykinter") window for a search function I made. While it searches, the function is supposed to update a text label every time it gets a usable result. The search function is for Mac. The code is way too long for this forum, so I copy-pasted into CollabEdit.
Here's the link.
http://collabedit.com/v7vjt
The function is called startSearch().

Code added by Mekire:
Code: Select all
from Tkinter import *
from ttk import Button
import os
from os.path import exists
from sys import argv
#-----
debug = False
if len(argv) > 1:
    if argv[1].lower() in ["-d", "-debug"]:
        debug = True
#-----
file = "mck_search_build.txt"
exists = exists(file)
if exists == False:
    if not debug:
        target = open(file, 'w')
        target.write("1")
        target.close()
    buildno = 1
elif exists == True:
    target = open(file, 'r')
    buildno = int(target.readline())
    if not debug:
        buildno += 1
    target.close()
#-----
class SearchApp(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, background = "white")
        self.parent = parent
        self.InitUI()

    def InitUI(self):
        self.parent.title("McKSearch GUI Build %d" % buildno)
        #self.style = Style()
        #self.style.theme_use("default")
        self.pack(fill = BOTH, expand = 1)
        self.SearchInApp = BooleanVar()
        self.SearchInApp.set(True)
        self.SearchFiles = BooleanVar()
        self.SearchFiles.set(True)
        self.SearchDirs = BooleanVar()
        self.SearchDirs.set(True)
        self.Query = StringVar()
        self.Query.set("")
        #self.results = IntVar()
        #self.results.set(0)

        canvas = Canvas(self)
        canvas.create_line(990, 1090, 990, 0, fill = "gray")
        canvas.pack(fill = BOTH, expand = 1)

        SearchInAppCheckButton = Checkbutton(self, text = "Search in .Apps:", variable = self.SearchInApp)#, command = self.onClickBool(self.SearchInApp))
        SearchInAppCheckButton.select()
        SearchInAppCheckButton.place(x = 1000, y = 60)
        SearchFilesCheckButton = Checkbutton(self, text = "Search for files", variable = self.SearchFiles)#, command = self.onclickBool(self.SearchFiles)
        SearchFilesCheckButton.select()
        SearchFilesCheckButton.place(x = 1000, y = 80)
        SearchDirsCheckButton = Checkbutton(self, text = "Search for directories", variable = self.SearchDirs)
        SearchDirsCheckButton.select()
        SearchDirsCheckButton.place(x = 1000, y = 100)

        buttonQuit = Button(self, text = "Quit", command = self.quit)
        buttonQuit.place(x = 1140, y = 20)
        buttonSearch = Button(self, text = "Search", command = self.startSearch)
        buttonSearch.place(x = 1000, y = 20)

        #radio
    def onClickBool(self, var):
        if var == True:
            self.master.title("Potato")

    def startSearch(self):
        label = Label(self, fg = "red")
        label.pack()
        query = ".app "
        global results
        results = 0
        for root, dirs, files in os.walk("/", topdown = False):
            if self.SearchFiles:
                for name in files:
                    location = os.path.join(root, name)
                    strlocation = str(location) + " "
                    if query.lower() in strlocation.lower():
                        if ".app/" not in strlocation.lower() or self.SearchInApp:
                            #print strlocation, "\n"
                            results += 1
                            label.config(text = str(results) + " results")

            if self.SearchDirs:
                for name in dirs:
                    location = os.path.join(root, name)
                    strlocation = str(location) + " "
                    if query.lower() in strlocation.lower():
                        if ".app/" not in strlocation.lower() or self.SearchInApp:
                            #print strlocation, "\n"
                            results += 1
                            label.config(text = str(results) + " results")
#-----
def main():
    x = (1366 / 2) - (1250 / 2)
    y = (768 / 2) - (720 / 2) - 11
    root = Tk()
    root.geometry("%dx%d+%d+%d" % (1250, 720, x, y))
    app = SearchApp(root)
    root.mainloop()

main()
#-----
if not debug:
    target = open(file, 'w')
    target.write(str(buildno))
    target.close()
Last edited by Mekire on Sat May 03, 2014 6:45 pm, edited 1 time in total.
Reason: Added the code; 100 lines is not so huge.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Tkinter counter Label not showing up until function ends

Postby Mekire » Sat May 03, 2014 7:03 pm

Tkinter only redraws the screen when the mainloop regains control (not mid function).

You can force a redraw by adding this after you change the label:
Code: Select all
self.update_idletasks()
But this is probably not an ideal solution.

-Mek
User avatar
Mekire
 
Posts: 984
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Tkinter counter Label not showing up until function ends

Postby mckryall » Sat May 03, 2014 7:13 pm

Thanks, Mek. Works perfectly. That'll help when I make the results box, too.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Tkinter counter Label not showing up until function ends

Postby jedii » Mon May 19, 2014 9:01 pm

@Mekire do you mind if I send you my code and see if you can implement it with tkinter code please
jedii
 
Posts: 1
Joined: Sun May 18, 2014 7:52 pm

Re: Tkinter counter Label not showing up until function ends

Postby mckryall » Mon May 26, 2014 5:23 am

Send him a PM.
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests