0

im learning Python and making GUI program with Tkinter. i want to update status bar's message 2 times when button clicked. 1st message is will update when button clicked. 2nd message will update after when program successfully work. The problem is that 1st message isnt showing at all when i click the button21

encryption_tab

def encryption_tab(self):
    self.input3 = tk.LabelFrame(self.tab1, bg="black", fg='white', text=" Source Image ")
    self.input3.pack(expand=1, fill='both', padx=5, pady=5)

    self.target_image_path = tk.StringVar()
    self.verb_ent = tk.Entry(self.input3, width=55, textvariable=self.target_image_path)
    self.verb_ent.pack(side=tk.LEFT, expand=1, fill="both", padx=5, pady=5)

    button2 = tk.Button(self.input3, text="Search Image", command=lambda: self.get_path(0))
    button2.pack(side=tk.LEFT, padx=5, pady=5)
    button2.configure(bg="black", fg='white', activebackground='#0080ff', activeforeground='white')

    self.input = tk.LabelFrame(self.tab1, text=" Hide text ",  background="black", foreground='white')
    self.textvar = tk.StringVar()
    self.textbox = tk.Text(self.input, height=5, width=70,  wrap='word', undo=True)
    self.textbox.pack(expand=1, fill="both", padx=5, pady=5)
    self.textbox.bind("<Key>", self.update)

    self.counter = tk.StringVar()
    self.counter.set('Одоогоор текст: ' +'0'+ ' тэмдэгттэй байна')
    self.char_count = tk.Label(self.input, textvariable=self.counter, bg="Black", fg= '#0080ff')
    self.char_count.pack(side=tk.LEFT, expand=1, fill="both", padx=5, pady=5)

    button21 = tk.Button(self.input, text="hide text", command=lambda: self.hide())
    button21.pack(side=tk.RIGHT, padx=5, pady=5)
    button21.configure(bg="black", fg='white', activebackground='#0080ff', activeforeground='white')
    #guess here's the problem tho
    self.input.pack(expand=1, fill="both", padx=5, pady=5)

    self.input1 = tk.LabelFrame(self.tab1, bg="black", fg='white', text=" Status ")
    self.status_message = tk.StringVar()
    self.status_message.set('\n program not yet started \n')

    textwidget = tk.Label(self.input1, textvariable=self.status_message, bg='black', fg="white", wraplength=590)
    textwidget.configure(relief='flat', state="normal")
    textwidget.pack(expand=1, fill="both", padx=5, pady=5)

    self.input1.pack(expand=1, fill="both", padx=5, pady=5)

here's hide() function

def hide(self):
    #this message is 1st... but not working tho
    self.status_update(" program is working...")
    #
    original_image_file = self.target_image_path.get()
    img = Image.open(original_image_file)

    import ntpath
    encoded_image_file = ntpath.split(original_image_file)[0] + "/enc_" + ntpath.basename(original_image_file)
    secret_msg = self.textbox.get("1.0", tk.END)        
    img_encoded = self.encode_image(img, secret_msg)

    if img_encoded:
        img_encoded.save(encoded_image_file)
        # this is 2nd message.. its working fine
        self.status_update("{} хадгалагдсан!".format(encoded_image_file))

        if sys.platform == "win32":
            os.startfile(encoded_image_file)
        else:
            opener ="open" if sys.platform == "darwin" else "xdg-open"
            subprocess.call([opener, encoded_image_file])

status_update function... maybe its seems useless... but i use this function in another tabs. and they're working fine.

def status_update(self, status):
    self.status_message.set(status)

when i call hide() function the 1st message not updating... after program work successfully, 2nd message is appearing only... Someone know issue? am i just made simple mistake?

sorry, my english isnt perfect :{, its my 1st time to asking from stackoverflow :)

Ulteara
  • 13
  • 3
  • Read [Freezing during the execution of a function](https://stackoverflow.com/questions/10847626/program-freezing-during-the-execution-of-a-function-in-tkinter) – stovfl Mar 09 '20 at 14:15
  • wow thanks my program is also freezing during execution... i see why this is happening now clearly :) tyvm – Ulteara Mar 11 '20 at 10:01

1 Answers1

0

When you click your button button21, it doesn't update and show the first message as you are missing root.update() after it. As an example, try the following:

import tkinter as Tk

def click():
    count = 0
    lbl.config(text='Button is clicked')
    root.update() # comment this to see the difference
    for i in range(100000000):
        count += 1
    lbl.config(text='Process finished')

root = Tk.Tk()

bt= Tk.Button(root, text='click me', command=click)
bt.pack()

lbl = Tk.Label(root, text='')
lbl.pack()

root.mainloop()

when root.update() is not called, the re-drawing of the widgets does not take place until unless the control is given back to the mainloop, which would generally take place after the hide function is executed

FrainBr33z3
  • 1,085
  • 1
  • 6
  • 12
  • ahh thanks, i just noticed that i created update() function by myself... thanks ur answer :) it helps me a lot. Used 2 function for update bar... 1st one is status_update() and last one is update()... Then i just deleted my update() function its working fine – Ulteara Mar 11 '20 at 10:00