0

Hi All,

I am trying to run a second script from my first script and this works ok i can send it a variable and recieve back my answer however I am using Tkinter for my forms but when i start the main my program both scripts run although i only want to run the main script first then call the second when required. I am using eclipse IDE with python 2.7 and when i add another script to the project i am asked whether it should be "blank" or "main" etc.
What do i need to add into the second script to stop executing straight away.
If this has been answered before please point me to that message
Thanks ALL.

2
Contributors
6
Replies
8
Views
4 Years
Discussion Span
Last Post by deepthought
0

All code must be inside functions, except any code you want only run when running directly, which you can put inside

  if __name__ == '__main__':
0

if name == 'main':

Hi Thanks but its monday and i think i am feeling thick today please explain a little more my actual main file is call ]ec main.py my second file is called RxTx.py. so i need to place if name == 'main': into the top of RxTx.py or is there something more to it.

I appreciate your patience Thanks

0

No, before the lines you do not want to run when importing it to the other script as you mentioned.

0

I think I may have the structure of my program wrong?
in the main i have created functions about 6 of them ie

def mCommand_Enter():
        mycom = RxTx.comms(Enter_Command)
        return

in the second script i have created another function:-

import socket
import time


def comms(TxMessage):
    UDP_IP = "192.168.60.3"
    UDP_PORT = 9008
    MESSAGE=TxMessage
    print ("UDP target IP:",UDP_IP)
    print ("UDP target port:",UDP_PORT)
    print ("message:",MESSAGE)
    sock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM ) # UDP
    #sock.connect((UDP_IP, UDP_PORT))
    #b = str(MESSAGE,'utf_8')
    sock.sendto(MESSAGE,(UDP_IP, UDP_PORT) )
    sock.close()


    UDP_IP_SERV="192.168.60.10"
    UDP_PORT_SERV=5634
    sock = socket.socket( socket.AF_INET, # Internet
                      socket.SOCK_DGRAM ) # UDP
    sock.bind( (UDP_IP_SERV,UDP_PORT_SERV) )

    #while True:
    time.sleep(1)
    data, addr = sock.recvfrom(782) # buffer size is 4096 bytes
    print "received message:- ", data,addr
    #mydata = data.encode("ascii")
    #print mydata
    #print(str(data,'utf_8',errors='strict'))
    sock.close()
    return

Back in the main program created a button

bStart = Button(mGui,text ='Connect',width = 8,bg ='green',command = mConnect()).place(x=550,y=500,)

When i run the program the button gets generated and runs the mConnect function which runs the code above (first snippet) if i hash # out the button code the main program runs without running the second script i dont understand why generating a button on the form should trip the code into running.

Thanks again

0

You are not defining command to run when Button is pushed but you have instead called the function, before pushing of the Button. It should not be with ()

0

Thanks PyTony well spotted it was a pure typo as earlier i was potential going to send a value across too but changed my mind and forgot to remove the ().
Great thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.