Am a new in GUI programming and I am trying to make a GUI for one of my python parser.
I know that :
Tkinter is single threaded. Screen updates happen on each trip through the event loop. Any time you have a long running command you are preventing the event loop from completing an iteration, thus preventing the processing of events, thus preventing redraws.
My program call a big function that takes about 5 minutes to be ran entirely. So I guess the only solution is tu use thread for the long running command.
BUT, my long running command in already threaded so I don't really know how to proceed.
--> As soon as I click on BUT1 in the GUI, the program freeze until the function is entirely done. I'd like to run this function in the backgroung, so the program will not freeze.
--> I'm not looking for a complete solution but if someone can put me on a good track, it will be wonderful !
Main.py -> The GUI
Module_1.py -> The function that we call by clicking on the button BUT1
Thank you in advance !
Here is Main.py --> the GUI
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import *
import sys
import tkMessageBox
import tkFileDialog
import Module_1
import csv
from time import strftime, gmtime
DATE = strftime("_%d_%b_%Y")
class App:
def __init__(self, master):
self.frame = Frame(master, borderwidth=5, relief=RIDGE)
self.frame.grid()
class IORedirector(object):
def __init__(self,TEXT_INFO):
self.TEXT_INFO = TEXT_INFO
class StdoutRedirector(IORedirector):
def write(self,str):
self.TEXT_INFO.config(text=self.TEXT_INFO.cget('text') + str)
self.TEXT_HEADER = self.text_intro = Label(self.frame, bg="lightblue",text="THIS IS \n MY SUPER PROGRAM")
self.TEXT_HEADER.grid(row=0, column=0, columnspan=2, sticky=W+E+N+S)
self.MENU = Frame(self.frame, borderwidth=5, relief=RIDGE, height=12)
self.MENU.grid(row=1, column=0, sticky=N)
self.button = Button(self.MENU, text="QUIT", bg="red", command=self.frame.quit)
self.button.grid(row=4, column=0)
self.BUT1 = Button(self.MENU, text="BUT1", command=self.BUT1)
self.BUT1.grid(row=0, column=0,sticky=W+E)
self.TEXT_INFO = Label(self.frame, height=12, width=40, text="SOME TEXT", bg="grey",borderwidth=5, relief=RIDGE)
self.TEXT_INFO.grid(row=1, column=1, sticky = N+W)
sys.stdout = StdoutRedirector(self.TEXT_INFO)
def BUT1(self):
self.BUT1.config(text="RUNNING")
self.TEXT_INFO.config(text="BUT1 LAUNCHED")
Module_1.main("BUT1")
## HERE WE NEED TO RUN THE FUNCTION
## THE PROGRAMM FREEZE HERE UNTIL THE FUNCTION IS ENTIRELY RUN
self.TEXT_INFO.config(text="BUT1 FINISHED")
self.BUT1.config(text="DONE")
root = Tk()
app = App(root)
root.mainloop()
And here is Module_1.py --> contain the big function
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Queue
import threading
import urllib2
import time
from bs4 import BeautifulSoup as soup
from urllib2 import urlopen
import re
import os
import random
import sys
import logging
import csv
from time import strftime, gmtime
import os
import random
import shutil
import sys
import re
import logging
from threading import RLock
from time import strftime, gmtime
import csv
import urllib
from urllib import urlretrieve
from grab.spider import Spider, Task
logging.basicConfig(level=logging.CRITICAL) # Loggin to DEBUG / INFO
log = logging.getLogger()
DATE = strftime("_%d_%b_%Y")
class SPIDER1(Spider):
initial_urls = ['URL_THAT_I_NEED_TO_PARSE']
def __init__(self):
super(SPIDER1, self).__init__(
thread_number=20,
network_try_limit=20,
task_try_limit=20
)
self.result = {}
def task_initial(self, grab, task):
for opt in grab.css_list("select[name='Template$TestCentreSearch1$SubRegionList'] option")[1:]:
grab.set_input('Template$TestCentreSearch1$SubRegionList', opt.attrib['value'])
grab.submit(extra_post={
'__EVENTTARGET': 'Template$TestCentreSearch1$SubRegionList'
}, make_request=False)
yield Task('parse', grab=grab, country=opt.text_content())
def task_parse(self, grab, task):
log.info('downloaded %s' % task.country)
city_gen = (x.text_content() for x in grab.css_list(".TestCentreSearchLabel+br+span"))
title_gen = (x.text_content() for x in grab.css_list(".TestCentreSearchTitle"))
id_gen = (x.attrib['href'][-36:] for x in grab.css_list(".TestCentreSearchLink"))
for x in zip(city_gen, title_gen, id_gen):
self.result[x[2]] = {
'country': task.country,
'city': x[0],
'name': x[1],
'id': x[2],
'price':'',
'currency':'',
'fee':''
}
yield Task('info', 'URL_URL=%s' % x[2], id=x[2])
def task_info(self, grab, task):
for label in grab.css_list(".TestCentreViewLabel"):
if label.text_content().strip()=="Test Fee:":
fees = label.getnext().text_content().strip()
self.result[task.id]['fee'] = fees
price = re.findall('\d[\d\., ]+\d',fees)
if price:
price = re.findall('\d[\d\., ]+\d',fees)[0]
self.result[task.id]['price'] = price.replace(' ','').replace(',','.')
currency = re.findall('[A-Z]{2,3}[$|€|£]?',fees)
if not currency:
currency = re.findall('[$|€|£]',fees)
if not currency:
currency = fees.replace(price,'').strip().replace(' ','')
if isinstance(currency,list):
currency = currency[0]
self.result[task.id]['currency'] = currency
#log.info(' %(price)s %(currency)s - %(fee)s ' % self.result[task.id])
break
def dump(self, path):
"""
Save result as csv into the path
"""
with open(path, 'w') as file:
file.write("ID;Country;State;City;Name;Price;Currency;Original Fee\n")
for test_center in sorted(self.result.values(), key=lambda x: "%(country)s%(city)s%(name)s" % x):
file.write(("%(id)s;%(country)s;;%(country)s;%(name)s;%(price)s;%(currency)s;%(fee)s\n" % test_center).encode('utf8'))
def main(choice):
parser, path, name = None, None, None
def run(name,parser,path):
log.info('Parsing %s...' % name)
parser.run()
parser.dump(path)
log.info('Parsing %s completed, data was dumped into %s' % (name, path))
log.info(parser.render_stats())
if choice == "NONE":
# DO NOTHING
# HERE I'D LIKE TO HAVE ANOTHER CALL TO ANOTHER THREADED FUNCTION
elif choice == "BUT1":
run('Function1',SPIDER1(),'C:\LOL\Output1'+DATE+'.csv')
So by clicking on BUT1, we run the main("BUT1") function contained in the Module_1.py file with argument BUT1 that launch -> run('Function1',SPIDER1(),'C:\LOL\Output1'+DATE+'.csv')
And then the program freeze until the parser has finished is work .. :)
The problem is simple: BUT1 won't return until the call to main returns. As long as main (and thus, BUT1) doesn't return, your GUI will be frozen.
For this to work you must put main in a separate thread. It's not sufficient that main spawns other threads if all it's doing is waiting for those threads.
If you call root.update() occasionally from the BUT1 function, that should prevent the GUI from freezing. You could also do that from a python thread with a fixed interval.
For example, updating every 0.1 seconds:
from threading import Thread
from time import sleep
self.updateGUIThread = Thread(target=self.updateGUI)
def updateGUI(self):
while self.updateNeeded
root.update()
sleep(0.1)
After the big function completes you can set self.updateNeeded to False.
Related
I'm having trouble decoding an image when I import it from another file.
I have four .py files to make two buttons.
In the first, on line 21, the button works and brings the image, in the second, on line 28, which is imported from another file, it appears as a button but without an image. How can I resolve this?
lab.py
import tkinter as tk
import base64
from imagens import *
from cores import *
from widgets import *
class Janela(Images):
def __init__(self) -> None:
self.images_base64()
self.tamJanela()
def tamJanela(self):
self.janela_doMenu = tk.Tk()
janela = self.janela_doMenu
# janela.state('zoomed')
janela.title('Disk Gás Gonçalves')
janela.configure(background=preto_claro)
janela.geometry("%dx%d" % (janela.winfo_screenwidth(), janela.winfo_screenheight()))
# Here the code works ###############################################################
self.img_icoName = tk.PhotoImage(data=base64.b64decode(self.editUser))
self.rotulo_nome2 = tk.Button(master=janela, image=self.img_icoName, activebackground='#00FA9A', bg='#4F4F4F',
highlightbackground='#4F4F4F', highlightcolor='#4F4F4F')
self.rotulo_nome2.grid(row=0, column=0)
# Here the code does not work #######################################################
# I'm importing from widgets.py #####################################################
Botoes(janela)
janela.minsize(1200, 640)
janela.mainloop()
Janela()
widgets.py
import tkinter as tk
import base64
from imagens import *
from cores import *
class Botoes(Images):
def __init__(self, local) -> None:
self.images_base64()
self.local = local
self.bt()
def bt(self):
self.img_icoName = tk.PhotoImage(data=base64.b64decode(self.editUser))
self.rotulo_nome2 = tk.Button(master=self.local, image=self.img_icoName, activebackground='#00FA9A', bg='#4F4F4F',
highlightbackground='#4F4F4F', highlightcolor='#4F4F4F')
self.rotulo_nome2.grid(row=1, column=0)
cores.py
preto_claro = '#4F4F4F'
verde_médio = '#00FA9A'
vermelho_salmão = '#FA8072'
azul_seleção = '#F0FFFF'
verde_limão = '#00FF00'
branco_gelo = '#EDEBE6'
imagens.py
import base64
class Images:
def images_base64(self):
self.addUser = 'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAd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Oqkk5BFHis3XRlhbprDrJ0xTpwBL6sQd5iQICAvWLo+pss2dWx/qt1hpHTYDACSoCH/+a9Kx7nfwAPPztG5sk4owAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMi0wOS0yMlQyMjoxNjoxNyswMDowMOu8Z3oAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjItMDktMjJUMjI6MTY6MTcrMDA6MDCa4d/GAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAABJRU5ErkJggg=='
I tried to create a button by importing the base64 image from another file.
There are two buttons in the application, one works and the image appears, the other does not work and the image does not appear. I would like to make the second one work.
All the code will generate this image. The bottom button that doesn't show the image is in the widgets.py file
I don't know why, but when I import it, it doesn't work.
Thank you in advance for your attention and I apologize if something was wrong or out of standard, my English is not very good.
It is because there is no variable referencing the instance of Botoes(), so it will be garbage collected (so as the image inside it).
Just use a variable to store the instance of Botoes():
class Janela(Images):
def __init__(self) -> None:
self.images_base64()
self.tamJanela()
def tamJanela(self):
self.janela_doMenu = tk.Tk()
janela = self.janela_doMenu
# janela.state('zoomed')
janela.title('Disk Gás Gonçalves')
janela.configure(background=preto_claro)
janela.geometry("%dx%d" % (janela.winfo_screenwidth(), janela.winfo_screenheight()))
# Here the code works ###############################################################
self.img_icoName = tk.PhotoImage(data=base64.b64decode(self.editUser))
self.rotulo_nome2 = tk.Button(master=janela, image=self.img_icoName, activebackground='#00FA9A', bg='#4F4F4F',
highlightbackground='#4F4F4F', highlightcolor='#4F4F4F')
self.rotulo_nome2.grid(row=0, column=0)
# need a variable to store the instance of Botoes
# to avoid garbage collection
botoes = Botoes(janela)
janela.minsize(1200, 640)
janela.mainloop()
Result:
I am developing an EEL project, and I needed to create a file dialog on the python side in order to preprocess data before sending it to javascript.
I tried to use tk.filedialog.askopenfilename, but that somehow froze the javascript event loop.
I found an answer on StackOverflow that used wxpython to create a non-blocking file picker. However, when I run the code below, the file picker always starts minimized.
However, once you use the file picker once, it works perfectly the second time.
Any help appreciated.
import base64
import json
from tkinter import Tk
Tk().withdraw()
from tkinter.filedialog import askopenfilename
import PIL.Image
import eel
import numpy as np
import wx
# Reusable wxpython App instance for the creation of non-blocking popup dialogs
app=wx.App(None)
eel.init("public")
def encode(bts):
return base64.b64encode(bts)
def array_to_json(array):
return json.dumps({
"shape": list(array.shape),
"dtype": str(array.dtype),
"data":list(np.ravel(array).astype(float)) # not efficient but quite clean
})
#eel.expose
def load_image(path):
return array_to_json(np.asarray(PIL.Image.open(path)))
#eel.expose
def pick_image():
# return askopenfilename()
""" --- Adapted from https://stackoverflow.com/a/59177064/5166365"""
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.STAY_ON_TOP | wx.DIALOG_NO_PARENT | wx.MAXIMIZE
dialog = wx.FileDialog(None, "Open File", wildcard="*", style=style)
dialog.Iconize(False)
dialog.Maximize()
dialog.Raise()
path = ""
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
else:
path = ""
return path
""" --- """
eel.start("index.html")
I was able to get it working with the following code. I used a regular window instead of a wx.Dialog or similar class.
class FancyFilePickerApplication:
def __init__(self):
self.app = wx.App()
self.frame = wx.Frame(None,title="Fancy File Picker")
self.build_ui()
##private
def build_ui(self):
self.vertical_sizer = wx.BoxSizer(wx.VERTICAL)
self.control_panel = wx.Panel(self.frame,wx.ID_ANY)
self.horizontal_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.control_panel.SetSizer(self.horizontal_sizer)
self.frame.SetSizer(self.vertical_sizer)
self.dir_ctrl = wx.GenericDirCtrl(self.frame,wx.ID_ANY,wx.EmptyString,wx.DefaultPosition,wx.Size(400,300))
self.vertical_sizer.Add(self.dir_ctrl,wx.SizerFlags().Expand())
self.vertical_sizer.Add(self.control_panel,wx.SizerFlags().Expand())
self.scale_factor_label = wx.StaticText(self.control_panel,wx.ID_ANY,"Scale Factor: ")
self.scale_factor_textbox = wx.TextCtrl(self.control_panel,wx.ID_ANY)
self.open_button = wx.Button(self.control_panel,wx.ID_ANY,"Open")
self.horizontal_sizer.Add(self.scale_factor_label,wx.SizerFlags().Expand())
self.horizontal_sizer.Add(self.scale_factor_textbox,wx.SizerFlags().Expand())
self.horizontal_sizer.Add(self.open_button,wx.SizerFlags().Expand())
self.open_button.Bind(wx.EVT_BUTTON,lambda evt:self.submit())
self.frame.Bind(wx.EVT_CLOSE,self.cancel)
def open(self, file_picked_callback):
self.file_picked_callback = file_picked_callback
self.frame.Fit()
self.frame.Center()
self.frame.Show()
self.frame.Raise()
self.frame.ToggleWindowStyle(wx.STAY_ON_TOP)
self.app.MainLoop()
##private
def submit(self):
filepath =self.dir_ctrl.GetFilePath()
scale_factor_text = self.scale_factor_textbox.GetValue()
scale_factor = 1.0 if not scale_factor_text.strip() else float(scale_factor_text)
self.file_picked_callback(filepath,scale_factor)
self.frame.Destroy()
##private
def cancel(self,evt):
self.file_picked_callback("",0)
self.frame.Destroy()
First I have to say that I just started Python programming. I do automatic inventory balance checking, mainly as a hobby.
I need help with my program...
This opens the excel file and looks for the value 0, in the B column.
if there is a 0 in the column which means that the part has run out, the code will send an e-mail with the title of the article from column A
When I did the debugging I noticed if i update the the Excel file that code uses in Exel at the same time the program happens to read the table, i get "Excel sharing violation error"
this is not a big problem because the final program will read the file once or twice a day but there is a small chance of an error, probably something else will crash, hopefully not.
I tried all these common ways with Exel
-Authorize Indexing for file Folders
-Permitting Sharing Wizard
-Saved file to a different location
-Renamed file
-i don't use third party Antivirus program
So would you have any tips on how i can improve the code, get rid of the above error and continue learning.
import threading
import tkinter as tk
from tkinter import BOTTOM,Label, Menu
import os
import smtplib
import datetime
import time
from time import sleep
import pandas as pds
root = tk.Tk()
root.title('Timer')
sec = 0
def timer():
global sec
sec += 1
time['text'] = sec
time.after(1000, timer)
time = Label(root, fg='black')
time.pack()
contact_num = os.environ.get("Contact_number")
contact_email = os.environ.get("Contact_email")
L = Label(root, text="Automatic warehouse alarm").pack()
L_2 = Label(root, text="START, Run program").pack()
root.title("Warehouse 1.4.2")
root.geometry("460x360")
class get_data:
def get_empty_stock(self):
while(True):
Email_adress = os.environ.get("email_user")
Email_password = os.environ.get("email.password")
file = ("C:\Exel_files\SpareParts_Vers25.xlsx")
try:
dataFrame = pds.read_excel(file)
except PermissionError:
print("PermissionError")
else:
pass
booleans = []
for Amount in dataFrame.Amount:
if Amount <= 0:
booleans.append(True)
else:
booleans.append(False)
if True in booleans:
empty_stock = dataFrame.loc[dataFrame.Amount <= 0]
with smtplib.SMTP("smtp.gmail.com",587) as smtp_server:
time_date = datetime.datetime.now()
time = time_date.strftime("%H:%M:%S")
date = time_date.strftime("%d/%m/%Y")
smtp_server.ehlo()
smtp_server.starttls()
smtp_server.ehlo()
smtp_server.login(Email_adress,Email_password)
subject = "Warehouse"
body = f"""{empty_stock}\nPart is out of stock
{date}\n
{time}\n
"""
msg = f"subject: {subject}\n\n{body}"
smtp_server.sendmail(Email_adress,Email_adress, msg)
sleep(25)
def __init__(self):
t = threading.Thread(target=self.get_empty_stock)
t.start()
Start = tk.Button(root, text = "START", width= 25, command=lambda:[get_data(),timer()]).pack()
Stop = tk.Button(root, text = "QUIT", width= 25, command= root.quit).pack(side=BOTTOM)
menu = Menu(root)
root.config(menu=menu)
helpmenu = Menu(menu)
menu.add_cascade(label='menu', menu=helpmenu)
helpmenu.add_command(label=contact_email)
helpmenu.add_command(label=contact_num)
helpmenu.add_command(label= "Exit", command=root.quit)
root.mainloop()
To avoid multi-user sharing violation, consider copying the last saved instance of Excel file using a tempfile and point read_excel to this temp file which is automatically deleted after the with context:
import shutil
import tempfile
...
file = "C:\Exel_files\SpareParts_Vers25.xlsx"
with tempfile.NamedTemporaryFile() as tmp:
# COPY EXCEL FILE TO TEMP FILE
shutil.copyfile(file, tmp.name)
try:
# READ IN TEMP FILE
dataFrame = pds.read_excel(tmp.name)
except PermissionError:
print("PermissionError")
I am trying to build a Tkinter UI with a button where the user chooses a main file and based on information in that file, the script would get some information about some secondary files. Upon choosing the main file, I am trying to use the file path of the main file to get the file path of the secondary files. However, because I am calling a function "open_main_file()" twice, I get two dialog boxes where the user will have to choose the main file twice instead of once. How can I circumvent that?
My code looks like this:
import pandas as pd
from tkinter import *
from os.path import dirname
from tkinter import filedialog
root = Tk()
def open_main_file():
return filedialog.askopenfilename()
def parse_main_file():
return pd.read_parquet(open_main_file(), engine='pyarrow')
def get_some_ids():
return parse_main_file()['some_ids'].iloc[0]
def get_list_of_other_files():
other_files_path = dirname(dirname(dirname(open_main_file())))
list_of_other_files = []
for f in get_some_ids():
list_of_other_files.append(glob.glob(other_files_path + '/identifier=' + str(f) + '/*'))
return map(''.join, list_of_other_files)
myButton = Button(root, text='Open File...', command=get_list_of_other_files).pack()
root.mainloop()
try not to use a function in other functions. In this case, you are calling a function one time in another one, and one time in the main code. That is why you get file dialog 2 times. Use the returned item and give it to the next function. You can code like this:
import pandas as pd
from tkinter import *
from os.path import dirname
from tkinter import filedialog
root = Tk()
def open_main_file(): # This function is not necessary
return filedialog.askopenfilename()
def parse_main_file(main_file_path):
return pd.read_parquet(main_file_path, engine='pyarrow')
def get_some_ids(main_file_path):
return parse_main_file(main_file_path)['some_ids'].iloc[0]
def get_list_of_other_files():
main_file_path = filedialog.askopenfilename()
other_files_path = dirname(dirname(dirname(main_file_path)))
list_of_other_files = []
for f in get_some_ids(main_file_path):
list_of_other_files.append(glob.glob(other_files_path + '/identifier=' + str(f) + '/*'))
return map(''.join, list_of_constituent_files)
myButton = Button(root, text='Open File...', command=get_list_of_other_files).pack()
root.mainloop()
I have the following program which is used to create a very basic GUI:
from __future__ import with_statement
from Tkinter import *
import tkFont
import test
class main_fun():
def init_proc(self):
stLabelStatus['text']="Started executing the test...."
time.sleep(5)
def procA(self):
print "inside ProcA...."
stLabelStatus['text'] = "Running ProcA...."
time.sleep(5)
def procB(self):
print "inside ProcB...."
stLabelStatus['text'] = "Running ProcB...."
time.sleep(5)
def procC(self):
print "inside ProcC...."
stLabelStatus['text'] = "Running ProcC...."
time.sleep(5)
def procD(self):
print "inside ProcD...."
stLabelStatus['text'] = "Running ProcD...."
time.sleep(5)
def procE(self):
print "inside ProcE...."
stLabelStatus['text'] = "Running ProcE...."
time.sleep(5)
def procF(self):
print "inside ProcF...."
stLabelStatus['text'] = "Running ProcF...."
time.sleep(5)
def main_control(self):
stLabelStatus.config(text='Running Tests....')
self.init_proc()
self.procA()
self.procB()
self.procC()
self.procD()
self.procE()
self.procF()
tests=main_fun()
stGUI = Tk()
stGUI.geometry("450x300+50+50")
stGUI.resizable(0,0)
stGUI.title("Testing GUI")
arial12 = tkFont.Font(root = stGUI, family='Arial', size=12, weight='bold')
arial16 = tkFont.Font(root = stGUI, family='Arial', size=16, weight='bold')
stLabelTestNumlbl = Label(stGUI,text="Test No.")
stLabelTestNumlbl['font'] = arial12
stLabelTestNumlbl.place(x=120,y=40)
stInputTestNum = Entry(stGUI, width=6)
stInputTestNum['font'] = arial12
stInputTestNum.place(x=250,y=40)
stLabelStatuslbl = Label(stGUI,text="Status")
stLabelStatuslbl['font'] = arial12
# stLabelStatus.pack()
# stLabelStatus.grid(row=0,column=0)
stLabelStatuslbl.place(x=20,y=180)
stLabelResultlbl = Label(stGUI,text="Result")
stLabelResultlbl['font'] = arial12
stLabelResultlbl.place(x=20,y=240)
stButtonStart = Button(stGUI,text="Start", command=tests.main_control)
stButtonStart['font'] = arial16
stButtonStart.place(x=180,y=110)
stLabelStatus = Label(stGUI, text="", anchor=CENTER)
stLabelResult = Label(stGUI, text="", anchor=CENTER)
stLabelStatus.place(x=90,y=183)
stLabelResult.place(x=90,y=243)
stGUI.mainloop()
Even though the GUI was created, I have a couple of issues here:
there is a small window titled tk that opens up along with my window titled Testing GUI (See the image below). I remember it was not there when I started out, looks like something that I recently added created it. I can't really understand exactly what though. How can I remove that tiny pest window or at least hide it?
I'm unable to update the Status label at runtime even though I've inserted stLabelStatus['text']="Running ProcA...." and similar statements as required in the flow. All that I can see is the very last line in the flow (procF is the last called function) stLabelStatus['text']="Running ProcF....".
How can I update the status during runtime as per my requirement? I'm sure its possible and some logical mistake I'm doing.
Sorry, I missed that command refers correctly to the tests object (of your custom class).
But this
#!python3
from tkinter import *
class main_fun():
def main_c():
print("main_c")
print(stLabelStatus)
stLabelStatus['text']="Status updated...."
tests=main_fun()
stGUI = Tk()
stGUI.geometry("450x300+50+50")
stGUI.resizable(0,0)
stGUI.title("Testing GUI")
stButtonStart = Button(stGUI,text="Start", command=tests.main_c)
stButtonStart.place(x=180,y=100)
stLabelStatus = Label(stGUI, text="", anchor=CENTER)
stLabelStatus.place(x=90,y=183)
stGUI.mainloop()
gives this output:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python35\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
TypeError: main_c() takes 0 positional arguments but 1 was given
which states an exact problem and probably would yield the answer in web search results.
But taking the hint at arguments, one will notice the class definition and its only method. You'd look at the Python documentation or tutorial on classes and notice that you're doing it wrong:
class does not have parameters, but the method must have at least one (self):
class main_fun:
def main_c(self):
print("main_c")
print(stLabelStatus)
stLabelStatus['text']="Status updated...."
Success!
Tested with a fresh Python 2.7.13:
#!python2
#coding=utf-8
from Tkinter import *
def my_method():
stLabelStatus['text']="Status updated...."
stGUI = Tk()
stButtonStart = Button(
stGUI,
text="Start",
command=my_method
)
stButtonStart.place(x=0, y=0)
stLabelStatus = Label(stGUI)
stLabelStatus.place(x=0, y=30)
stGUI.mainloop()