0

I'm trying to make GUI for scipt that reacts to pairCreated event of PancakeFactroy smart contract. Here's my code:

from tkinter import *
from web3 import Web3
import json
import asyncio

window = Tk()
window.geometry('500x250')
window.title('pair created')

factory = '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73' 
  
bsc = 'wss://speedy-nodes-nyc.moralis.io/********************************'                                                  
w3 = Web3(Web3.WebsocketProvider(bsc)

with open('D:/BOT/BOT/python/ABI/factoryABI.txt') as file:                            
     factory_abi = json.load(file)

contractF = w3.eth.contract(address = factory, abi = factory_abi)      

def handle_event(event):                                                                
    pairData = (toDict(event))                                                                               
    now = datetime.datetime.now()
    print('======== Pair created ========', now.strftime("%H:%M:%S %Y-%m-%d"))          
    print('Block number:',w3.eth.block_number)                                          
    print('token0:',pairData['args']['token0'])                                         
    print('token1:',pairData['args']['token1'])                                         
   

async def log_loop(event_filter, poll_interval):                              
    while True:
        for PairCreated in event_filter.get_new_entries():
            handle_event(PairCreated)
        await asyncio.sleep(poll_interval)

def main():                                                                   
    event_filter = contractF.events.PairCreated.createFilter(fromBlock='latest')
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(
            asyncio.gather(
                log_loop(event_filter, 1)))
    finally:
       loop.close()

buttonStart = Button(window, text = 'Start',command = main())
buttonStart.pack()
window.mainloop()

The problem is that script works without tkinter but when I try to create a window with a button that'll start the script, script just runs like there's no tkinter(printing pairs in the terminal and not creating a window).

0 Answers0