How do I run a web routine within Tkinter without crashing the mainloop?

Asked

Viewed 146 times

0

Good day I created an application by Tkinter and now I’m trying to make the information on the web available to users in real time. The problem is that the app.run function does not let you run the mainloop. It would have another way to do?

#Rotina Principal
#=============================================================================

janela = Tk()
janela.geometry ('1480x650')
janela.title('Sistema de Classificação de Defeitos')

quadro01 = Frame (janela, width = 1680, height = 600,relief = 'raise', bd = 10)
quadro01.pack(side = TOP)

quadro01a = Frame (janela, width = 1680, height = 300,relief = 'raise', bd = 10)
quadro01a.pack(side = BOTTOM)

quadro01b = Frame (quadro01, relief = 'raise')
quadro01b.pack()

quadro02 = Frame (janela, width = 700, height = 500,relief = 'raise', bg = 'yellow', bd = 8)
quadro02.pack(side = LEFT)

quadro03 = Frame (quadro02, width = 500, height = 1000,relief = 'raise', bg = 'yellow', bd = 8)
quadro03.pack()

quadro04 = Frame (janela, width = 700, height = 500,relief = 'raise', bg = 'purple', bd = 8)
quadro04.pack(side = LEFT)

quadro05 = Frame (quadro04, width = 500, height = 300,relief = 'raise', bg = 'purple', bd = 8)
quadro05.pack()

texto01 = Label (quadro01, text = variaveis_rotina['msg'], font = 'Arial 36 bold',fg = 'blue', width = 48)
texto01.pack(side = TOP)

texto02 = Label (quadro03, text = variaveis_rotina['area'], font = 'Arial 100 bold', bg = 'yellow', fg = 'purple', width = 8)
texto02.pack()

texto03 = Label (quadro05, text = variaveis_rotina['defeito'], font = 'Arial 100 bold', bg = 'purple', fg = 'yellow', width = 8)
texto03.pack()

b1 = Button (quadro01, text = variaveis_rotina['msg_button'], font = 'Arial 24 bold',bg = 'blue', fg = 'yellow',command = proximo)
b1.pack(side = RIGHT)

if (variaveis_rotina['passo'] == 0 or variaveis_rotina['passo'] == 30) :
    b2 = Button (quadro01, text = 'Não', font = 'Arial 24 bold',bg = 'blue', fg = 'yellow',command = sair)
    b2.pack(side = LEFT)

texto01a = Label (quadro01a, text = 'Contagem: ' + str (variaveis_rotina['contCourosGeral']), font = 'Arial 20 bold',fg = 'blue', width = 48)
texto01a.pack(side = LEFT)

texto01b = Label (quadro01a, text = 'Couros avaliados: ' + str (variaveis_rotina['contCourosAvaliados']), font = 'Arial 20 bold',fg = 'blue', width = 48)
texto01b.pack(side = RIGHT)
#========================================================
# Inseri essa parte para disponibilizar algumas informações pela web ainda estou testando mas percebi que app.run não deixa executar o mainloop e por consequencia não entra na tela principal
app = Flask (__name__)

@app.route ('/')

def inicio () :
    global variaveis_rotina
    return render_template ('Inicio.html', msg = 'A contagem é ' + str (variaveis_rotina['contCourosGeral']) )

if __name__ == '__main__' :
    print ("Servidor: " + subprocess.getoutput('hostname -I').strip() + ":5000")
    app.run(debug = True, host = '192.168.1.10') # essa linha parece que entra em outro loop e não deixa executar o mainloop
#=================================================================

janela.mainloop()
  • 1

    And why don’t you do it in different files, with different processes, with the information you want to make available in the database? And read about threads...

1 answer

0

Utilize threads to run parallel processes. See this example:

from threading import Thread

# Seu código...

if __name__ == '__main__' :
    print ("Servidor: " + subprocess.getoutput('hostname -I').strip() + ":5000")
    Thread(target=lambda:app.run(debug = True, host = '192.168.1.10')).start()
#=================================================================

janela.mainloop()

What threads do is perform multiple tasks at the same time. That way you don’t have to wait to finish running app.run to finally open the mainloop.

It is important to note that in some functions or methods, threads may end up generating problems. These problems are caused most of the time because the method you want to run (for example the method Icon.run of pystray) needs the execution to be done in the main thread. I am saying this because I do not know the Flask but it’s worth trying to use Thread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.