Python Forum
Which GUI can have indefinite loop ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Which GUI can have indefinite loop ?
#1
I just tried Tkinter, and any code after mainloop() is ignored. So I searched some more ways and found out that I can use THREAD.

My question is, can I use Tkinter mainloop() and use thread for my while 1 loop that runs indefinitely ? I need to be able to use the GUI, but at the same time I need to gather some data from my own loop. Is it doable ? I'm very new to this I just started 3 days ago, so I cannot try it myself yet, I have to find out how to do it first.

Or if there is a better GUI that can work with what I need, which one is it coz I read there are many other GUI.
Reply
#2
Tkinter has it's own infinity loop. Any data can be gathered through it. It would help to have an example of what you are after.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Code after mainloop() is not ignored. It runs after mainloop() is done. However, mainloop() is not done until the root window is destroyed.

What are you trying to do in your loop? Depending on what it is, you might want a thread, or you could use tkinter.after(time, function) to call a function after some time has passed. That function can use after() to continue calling itself.

There are lots of GUI's and they all have a loop like mainloop(). Often this is implemented as a function that blocks untill all the windows are destroyed. Some GUI's force you to write your own loop.
Reply
#4
I am trying to make a trading bot for meta trader 5.

The GUI part will only show the up and down prices. (the color blue when price is up and red when price is down), my sensor for buying is using price ups of several symbols. Thats the only thing I want for the GUI.

But then I want the infinity loop for the bot itself, but with tkinter I dont know how to make the loop for my main code, while that GUI is still detecting my buying sensor.
Reply
#5
I am not going to click on that link.

Sounds like you want to periodically update displays to show information you get from MetaTrader. You probably want to use a thread to get the MetaTrader information. You don't want the GUI to be unresponsive while this happens. You'll also need a way for the MetaTrader thread to tell the application it is time to update displays. The MetaTrader thread cannot do this itself, so it will have to set a signal flag (semaphore) that the GUI will periodically check (using the .after() method).
Reply
#6
(Nov-15-2023, 07:17 PM)deanhystad Wrote: I am not going to click on that link.

Sounds like you want to periodically update displays to show information you get from MetaTrader. You probably want to use a thread to get the MetaTrader information. You don't want the GUI to be unresponsive while this happens. You'll also need a way for the MetaTrader thread to tell the application it is time to update displays. The MetaTrader thread cannot do this itself, so it will have to set a signal flag (semaphore) that the GUI will periodically check (using the .after() method).
Yes. But it's constantly not periodically. The GUI is for displaying the data from the loop. That's exactly what I was thinking too, using the thread for getting the data.

Can .after() be set in milisecond ? So it updates almost realtime, not delayed.
Reply
#7
I am stuck.

This code is giving me an error :
If EURCHFlasttick == 0 :
^^^^^^^^^^^^^^
SyntaxError: invalid syntax

I've searched everywhere but mostly SyntaxError is caused by missing one equal , while I've correctly use double equal.

Can anyone tell me what's wrong in this code why is it giving me that error msg ?


# Copyright 2023, MetaQuotes Ltd.
# https://www.mql5.com
import threading
import MetaTrader5 as mt5
from datetime import datetime
from sysconfig import get_paths as gp; print(gp()['include'])
account=123
password="pass"
server="demoserver"
authorized=mt5.initialize(path="C:/Program Files/MetaTrader 5/terminal64.exe",login=account, server="demoserver",password="pass")

from tkinter import * 

root = Tk()
root.title("Color Check")
root.geometry("300x500")

EURCHFcolor=StringVar()
EURGBPcolor=StringVar()
EURJPYcolor=StringVar()
EURNZDcolor=StringVar()

EURCHFcolor.set('Blue')
EURGBPcolor.set('Blue')
EURJPYcolor.set('Blue')
EURNZDcolor.set('Blue')
 
EURCHFlabel=Label(root, text="EURCHF").grid(row=0,column=0)
EURGBPlabel=Label(root, text="EURGBP").grid(row=1,column=0)
EURJPYlabel=Label(root, text="EURJPY").grid(row=2,column=0)
EURNZDlabel=Label(root, text="EURNZD").grid(row=3,column=0)

EURCHFcolorlabel=Label(root, textvariable=EURCHFcolor).grid(row=0,column=1)
EURGBPcolorlabel=Label(root, textvariable=EURGBPcolor).grid(row=1,column=1)
EURJPYcolorlabel=Label(root, textvariable=EURJPYcolor).grid(row=2,column=1)
EURNZDcolorlabel=Label(root, textvariable=EURNZDcolor).grid(row=3,column=1)

EURCHFlasttick=0

def CheckPrice(symbol):
    price = mt5.symbol_info_tick(symbol).ask
    If EURCHFlasttick == 0 :
        EURCHFlasttick=price        
    else: 
        If EURCHFlasttick<price:
            EURCHFcolor.set('Blue',fg="Blue")            
        elif EURCHFlasttick>price:
            EURCHFcolor.set('Red',fg="Red")                
    

CheckPrice("EURCHF.GK")

root.mainloop()
 
mt5.shutdown()
Reply
#8
if, not If
Reply
#9
(Nov-15-2023, 09:21 PM)deanhystad Wrote: if, not If

Right, thanks!
Reply
#10
How about this error message ?

 if EURCHFlasttick == 0 :
UnboundLocalError: local variable 'EURCHFlasttick' referenced before assignment

I've tried adding
global EURCHFlasttick 
but it's still giving me an error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Init an indefinite number of class MathisDELAGE 9 2,317 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  Indefinite loop ( I think ) marsh20 2 1,935 Aug-20-2020, 12:33 PM
Last Post: deanhystad
  indefinite loop Johnygo 3 2,148 Jul-03-2019, 12:53 PM
Last Post: Johnygo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020